@fugood/buttress-server 2.24.2 → 2.25.0-beta.6
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 +19 -0
- package/config/sample.toml +6 -0
- package/lib/index.d.mts +2 -0
- package/lib/index.mjs +4 -6
- package/package.json +2 -2
- package/public/lib/index.d.ts +27 -0
- package/public/lib/index.mjs +110 -0
package/README.md
CHANGED
|
@@ -157,6 +157,25 @@ Examples:
|
|
|
157
157
|
bricks-buttress --test-caps ggml-stt --test-caps-model-id BricksDisplay/whisper-ggml:ggml-small.bin
|
|
158
158
|
```
|
|
159
159
|
|
|
160
|
+
## Compatibility Endpoints (Experimental)
|
|
161
|
+
|
|
162
|
+
The server can expose OpenAI- and Anthropic-compatible HTTP endpoints in addition to the native RPC. Each endpoint is opt-in via the TOML config:
|
|
163
|
+
|
|
164
|
+
```toml
|
|
165
|
+
[openai_compat]
|
|
166
|
+
enabled = true
|
|
167
|
+
# cors_allowed_origins = "*" # Or a list of origins; defaults to disabled
|
|
168
|
+
|
|
169
|
+
[anthropic_messages]
|
|
170
|
+
enabled = true
|
|
171
|
+
# cors_allowed_origins = ["http://localhost:3000"]
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
| Endpoint | Config flag |
|
|
175
|
+
| --------------------- | ------------------------------------ |
|
|
176
|
+
| `/oai-compat/v1/*` | `[openai_compat] enabled = true` |
|
|
177
|
+
| `/anthropic-messages` | `[anthropic_messages] enabled = true` |
|
|
178
|
+
|
|
160
179
|
## Session State Cache
|
|
161
180
|
|
|
162
181
|
The server supports session state caching for ggml-llm generators, which saves KV cache state to disk after completions. This enables:
|
package/config/sample.toml
CHANGED
|
@@ -17,9 +17,15 @@ log_level = "info"
|
|
|
17
17
|
# max_body_size = "100MB" # Supports string (e.g., "100MB", "1GB") or number in bytes
|
|
18
18
|
|
|
19
19
|
[openai_compat]
|
|
20
|
+
# enabled = true
|
|
20
21
|
# cors_allowed_origins = ["http://localhost:3000", "https://example.com"] # Restrict to specific origins
|
|
21
22
|
# cors_allowed_origins = "*" # Allow all origins (default)
|
|
22
23
|
|
|
24
|
+
[anthropic_messages]
|
|
25
|
+
# enabled = true
|
|
26
|
+
# cors_allowed_origins = ["http://localhost:3000", "https://example.com"]
|
|
27
|
+
# cors_allowed_origins = "*"
|
|
28
|
+
|
|
23
29
|
[runtime]
|
|
24
30
|
cache_dir = "./.buttress-cache"
|
|
25
31
|
# huggingface_token = "hf_xx"
|
package/lib/index.d.mts
CHANGED
|
@@ -229,9 +229,11 @@ type GeneratorConfig = {
|
|
|
229
229
|
type GlobalConfig = {
|
|
230
230
|
runtime?: RuntimeConfig;
|
|
231
231
|
openai_compat?: {
|
|
232
|
+
enabled?: boolean;
|
|
232
233
|
cors_allowed_origins?: string | string[];
|
|
233
234
|
};
|
|
234
235
|
anthropic_messages?: {
|
|
236
|
+
enabled?: boolean;
|
|
235
237
|
cors_allowed_origins?: string | string[];
|
|
236
238
|
};
|
|
237
239
|
} & Record<string, any>;
|
package/lib/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@ from huggingface_hub import snapshot_download
|
|
|
6
6
|
path = snapshot_download("${s}", revision="${o.model.revision||`main`}")
|
|
7
7
|
print(path)
|
|
8
8
|
`.trim(),a={...process.env};o.runtime.huggingface_token&&(a.HF_TOKEN=o.runtime.huggingface_token);let c=await q(t,[`-c`,n],{timeout:6e5,env:a});r?.(1);let l=c.stdout.trim().split(`
|
|
9
|
-
`).pop();i?.({localPath:l,repoId:s,alreadyExists:!1})}catch(e){throw a?.(e),e}finally{t?.deleteDownload(l)}})();return t?.setDownload(l,u),{started:!0,localPath:null,repoId:s}}async function J(e,t=null,n={}){if(e===`ggml-llm`)return cn(t,n);if(e===`ggml-stt`)return Yn(t,n);if(e===`mlx-llm`)return kr(t,n);throw Error(`Unknown backend type: ${e}`)}var Y=`@fugood/buttress-backend-core`,X=`2.
|
|
9
|
+
`).pop();i?.({localPath:l,repoId:s,alreadyExists:!1})}catch(e){throw a?.(e),e}finally{t?.deleteDownload(l)}})();return t?.setDownload(l,u),{started:!0,localPath:null,repoId:s}}async function J(e,t=null,n={}){if(e===`ggml-llm`)return cn(t,n);if(e===`ggml-stt`)return Yn(t,n);if(e===`mlx-llm`)return kr(t,n);throw Error(`Unknown backend type: ${e}`)}var Y=`@fugood/buttress-backend-core`,X=`2.25.0-beta.5`,jr={name:Y,private:!0,type:`module`,version:X,main:`src/index.js`,types:`lib/types/index.d.ts`,scripts:{build:`tsc --noCheck --declaration --emitDeclarationOnly --allowJs --outDir lib/types src/index.js`},dependencies:{"@fugood/buttress-hardware-guardrails":`^2.25.0-beta.5`,"@fugood/llama.node":`^1.7.0-rc.11`,"@fugood/whisper.node":`^1.0.18`,"@huggingface/gguf":`^0.3.2`,"@iarna/toml":`^3.0.0`,bytes:`^3.1.0`}};const Mr=e=>{if(!e)return{repoId:null,filename:null};let[t,n]=e.split(`:`);return{repoId:t,filename:n||null}};async function Nr({modelIds:e=[],defaultConfig:t=null}={}){let n=[];console.log(`${Y} v${X}`),console.log(`Generating model capabilities comparison...
|
|
10
10
|
`),n.push(`${Y} v${X}`),n.push(`## Model Capabilities Comparison
|
|
11
11
|
`),(!e||e.length===0)&&(console.error(`Error: No model IDs provided`),process.exit(1));try{let r=(e={},t={})=>{let n=Array.isArray(e)?[...e]:{...e};return Object.entries(t||{}).forEach(([e,t])=>{t&&typeof t==`object`&&!Array.isArray(t)?n[e]=r(n[e]||{},t):n[e]=t}),n},{server:i,generators:a=[],...o}=t||{},s=e=>r(structuredClone(o),e||{}),c=e=>{if(Array.isArray(a)&&a.length>0){let t=a.filter(e=>e?.type===`ggml-llm`);if(t.length>0&&e){let n=t.find(t=>t.model?.repo_id===e);if(n)return s(n)}}return Object.keys(o).length>0?s({}):null},l=[];for(let t=0;t<e.length;t+=1){let n=e[t];console.log(`[${t+1}/${e.length}] Analyzing ${n}...`);let r=c(n);r={...r||{},model:{...o.runtime,...r?.model||{},repo_id:n}};let i=await J(`ggml-llm`,null,{config:r,includeBreakdown:!0});l.push({modelId:n,capabilities:i,modelInfo:i.buttress?.selected||null,modelConfig:i.modelConfig||null})}let u=e=>e?(e/1024/1024/1024).toFixed(2):`N/A`,d=e=>e?`✅`:`🚫`;n.push(`| Model ID | Quantization | Size (GB) | Context Size | KV Cache Size (GB) | Total Required Memory (GB) | Fits GPU (Full) | Fits CPU (Full) |`),n.push(`|----------|--------------|-----------|--------------|--------------------|-----------------------------|-----------------|-----------------|`),l.forEach(({modelId:e,modelInfo:t,modelConfig:r})=>{let i=t?.quantization?.name?.toUpperCase()||`N/A`,a=u(t?.modelBytes),o=r?.nCtx||t?.kvInfo?.nCtxTrain||`N/A`,s=Ue(t),c=Number(o),l=t?.kvCacheBytes||(s&&Number.isFinite(c)&&c>0?s(c):s&&s(t?.kvInfo?.nCtxTrain||0))||null,f=u(l),p=u(t?.modelBytes&&l?t.modelBytes+l:t?.fit?.totalRequiredBytes),m=d(t?.fit?.fitsInGpu),h=d(t?.fit?.fitsInCpu);n.push(`| ${e} | ${i} | ${a} | ${o} | ${f} | ${p} | ${m} | ${h} |`);let g=t?.memoryLimitedCtx!=null||t?.limitedFit!=null,_=!t?.fit?.fitsInGpu||!t?.fit?.fitsInCpu;if(g&&_){let e=t?.memoryLimitedCtx||o,r=Number(e),i=t?.limitedKvCacheBytes||s&&Number.isFinite(r)&&r>0&&s(r)||null,c=u(i),l=u(t?.modelBytes&&i?t.modelBytes+i:t?.limitedFit?.totalRequiredBytes),m=d(t?.limitedFit?.fitsInGpu),h=d(t?.limitedFit?.fitsInCpu);(e!==o||c!==f||l!==p)&&n.push(`| ↳ Limited | - | ${a} | ${e} | ${c} | ${l} | ${m} | ${h} |`)}}),n.push(`
|
|
12
12
|
---`),n.push(`
|
|
@@ -33,7 +33,7 @@ print(path)
|
|
|
33
33
|
=== Hardware Information ===`);let t=null;if(process.platform!==`win32`)try{t=O(`uname -a`,{encoding:`utf8`}).trim()}catch{}t?console.log(`System: ${t}`):(console.log(`Hostname: ${v.hostname()}`),console.log(`OS: ${v.type()} ${v.release()}`)),console.log(`Platform: ${e.platform}`),console.log(`CPU Cores: ${v.cpus().length}`),console.log(`Total System Memory: ${(v.totalmem()/1024/1024/1024).toFixed(2)} GB`);let n=e.cpuTotalBytes>0?(e.cpuUsableBytes/e.cpuTotalBytes*100).toFixed(0):0;console.log(`Usable CPU Memory: ${(e.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${n}% of ${(e.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),e.hasGpu?(console.log(`
|
|
34
34
|
--- GPU Details ---`),e.devices.filter(e=>e.type===`gpu`).forEach(t=>{console.log(`GPU Backend: ${t.backend}`),console.log(`GPU Name: ${t.deviceName}`),console.log(`GPU Total Memory: ${(t.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let n=e.gpuTotalBytes>0?(e.gpuUsableBytes/e.gpuTotalBytes*100).toFixed(0):0;console.log(`GPU Usable Memory: ${(e.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${n}% of ${(e.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),t.metadata&&(t.metadata.hasBFloat16&&console.log(`Supports BFloat16: Yes`),t.metadata.hasUnifiedMemory&&console.log(`Unified Memory: Yes`))})):console.log(`GPU: Not available`),console.log(`\nBackend Variant: ${e.variant}`),console.log(`Performance Score: ${e.score}`),e.fit&&(console.log(`
|
|
35
35
|
--- Model Fit Analysis ---`),console.log(`Fits in GPU: ${e.fit.fitsInGpu?`Yes`:`No`}`),console.log(`Fits in CPU: ${e.fit.fitsInCpu?`Yes`:`No`}`),console.log(`Limiting Factor: ${e.fit.limiting}`))}console.log(`
|
|
36
|
-
=== Full Capabilities JSON ===`),console.log(JSON.stringify(u,null,2)),process.exit(0)}catch(e){console.error(`Failed to get capabilities:`,e.message),process.exit(1)}}var Lr=e({finalizeGenerator:()=>Vr,generatorRegistry:()=>Z,getCapabilities:()=>J,getModelIdentifier:()=>Gr,ggmlLlm:()=>Hr,ggmlStt:()=>Wr,globalDownloadManager:()=>Rr,mlxLlm:()=>Ur,showModelsTable:()=>Nr,showSttModelsTable:()=>Fr,startGenerator:()=>Br,startModelDownload:()=>qr,status:()=>Kr,testGgmlLlmCapabilities:()=>Pr,testGgmlSttCapabilities:()=>Ir});const Z=new Map,Rr={downloads:new Map,getDownload(e){return this.downloads.get(e)||null},setDownload(e,t){this.downloads.set(e,t)},deleteDownload(e){this.downloads.delete(e)},isDownloading(e){return this.downloads.has(e)},getActiveDownloads(){return Array.from(this.downloads.entries()).map(([e,t])=>({localPath:e,promise:t}))}},zr=e=>{let t=Z.get(e);if(!t)throw Error(`Unknown generator id "${e}"`);return t},Q=(e,t)=>{let n=zr(e);if(n.type!==t)throw Error(`Generator "${e}" does not support ${t} backend`);return n.instance};async function Br(e,t){let n={"ggml-llm":{create:nn,getId:rn},"ggml-stt":{create:Gn,getId:Kn},"mlx-llm":{create:Dr,getId:Or}}[e];if(!n)throw Error(`Unsupported backend type: ${e}`);let r=n.getId(t);if(!r)throw Error(`Buttress generator config missing repo identifier`);let i=`${e}:${r}`,a=Z.get(i);if(a)return a.refCount+=1,a.instance.resetFinalized?.(),{id:a.id,info:a.instance.info};let o=await n.create(i,t,{globalDownloadManager:Rr}),s={id:i,type:o.type,instance:o,refCount:1};return Z.set(i,s),{id:i,info:o.info}}async function Vr(e){let t=Z.get(e);return t?(--t.refCount,t.refCount<=0&&(await t.instance.finalize(),(t.instance.hasPendingReleases?.()??!1)||Z.delete(e)),!0):!1}const Hr={async initContext(e,t){return Q(e,`ggml-llm`).initContext(t)},async completion(e,t){return Q(e,`ggml-llm`).completion(t)},async tokenize(e,t){return Q(e,`ggml-llm`).tokenize(t)},async detokenize(e,t){return Q(e,`ggml-llm`).detokenize(t)},async applyChatTemplate(e,t){return Q(e,`ggml-llm`).applyChatTemplate(t)},async releaseContext(e,t){let n=Z.get(e);if(!n)return{released:!0,alreadyReleased:!0};if(n.type!==`ggml-llm`)throw Error(`Generator "${e}" does not support ggml-llm backend`);return n.instance.releaseContext(t)}},Ur={async initContext(e,t){return Q(e,`mlx-llm`).initContext(t)},async completion(e,t){return Q(e,`mlx-llm`).completion(t)},async tokenize(e,t){return Q(e,`mlx-llm`).tokenize(t)},async detokenize(e,t){return Q(e,`mlx-llm`).detokenize(t)},async applyChatTemplate(e,t){return Q(e,`mlx-llm`).applyChatTemplate(t)},async releaseContext(e,t){let n=Z.get(e);if(!n)return{released:!0,alreadyReleased:!0};if(n.type!==`mlx-llm`)throw Error(`Generator "${e}" does not support mlx-llm backend`);return n.instance.releaseContext(t)}},Wr={async initContext(e,t){return Q(e,`ggml-stt`).initContext(t)},async transcribe(e,t){return Q(e,`ggml-stt`).transcribe(t)},async transcribeData(e,t){return Q(e,`ggml-stt`).transcribeData(t)},async releaseContext(e,t){let n=Z.get(e);if(!n)return{released:!0,alreadyReleased:!0};if(n.type!==`ggml-stt`)throw Error(`Generator "${e}" does not support ggml-stt backend`);return n.instance.releaseContext(t)}};function Gr(e,t){return e===`ggml-llm`?rn(t):e===`ggml-stt`?Kn(t):e===`mlx-llm`?Or(t):null}const Kr={getFullStatus:()=>et(Z),getGgmlLlmStatus:()=>Ze(Z),getGgmlSttStatus:()=>Qe(Z),getMlxLlmStatus:()=>$e(Z),subscribeToStatus:Ye,subscribeToStatusWithId:Xe,llmStatusTracker:H,sttStatusTracker:U,statusEmitter:V};async function qr(e,t,n={}){let r={"ggml-llm":an,"ggml-stt":qn,"mlx-llm":Ar}[e];return r?r(t,Rr,n):{started:!1,localPath:null,repoId:null,error:`Unknown backend type: ${e}`}}var Jr=`@fugood/buttress-server`,Yr=`2.24.1`,Xr={name:Jr,version:Yr,main:`lib/index.mjs`,types:`lib/index.d.mts`,type:`module`,bin:{"bricks-buttress":`./bin/bricks-buttress`},files:[`lib`,`bin`,`config`,`public`],scripts:{typecheck:`tsc --noEmit`,build:`tsdown -c rolldown.config.js`,prepublish:`bun run build`,dev:`bun src/index.ts`,start:`bun lib/index.mjs`,"start-with-node":`node lib/index.mjs`},keywords:[`BRICKS`,`buttress`,`server`],license:`MIT`,dependencies:{"@elysiajs/cors":`^1.1.1`,"@elysiajs/node":`^1.4.2`,"@fugood/llama.node":`^1.7.0-rc.11`,"@fugood/whisper.node":`^1.0.18`,"@huggingface/gguf":`^0.3.2`,"@iarna/toml":`^3.0.0`,bytes:`^3.1.0`,elysia:`^1.4.19`,ms:`^2.1.1`,"node-machine-id":`^1.1.12`,zod:`^3.25.76`},devDependencies:{tsdown:`^0.20.1`,typescript:`^5.9.3`},gitHead:`984a440ed04862f12c65f3cf62bdc70a938fcdd6`};const Zr=()=>({version:Yr,name:Jr,description:Xr.description}),Qr=typeof process<`u`&&process.versions&&process.versions.node,$r=e=>new n({adapter:Qr?t():void 0,...e}),ei=a.Object({id:a.String(),name:a.String(),version:a.String(),generators:a.Array(a.Object({type:a.String()})),authentication:a.Object({required:a.Boolean(),type:a.Literal(`device-group`)})}),ti=({store:{serverInfo:e}})=>({id:e.id,name:e.name,version:e.version,generators:e.generators,authentication:e.authentication});var ni=e=>{let t=$r(),n=e.autodiscover.http?.path??`/buttress/info`;return t.get(n,ti,{response:ei}),t};const ri=typeof process<`u`&&process.versions!=null&&process.versions.node!=null;var ii=$r().post(`/buttress/upload`,async({body:{file:e},store:{config:t}})=>{let n=`${Date.now()}-${e.name.replace(/[^\dA-Za-z]/g,`_`)}`,r=_.join(t.server.temp_file_dir,n);try{return ri?await g(r,await e.stream()):await g(r,await e.arrayBuffer()),{ok:!0,filename:n}}catch(e){return{ok:!1,error:String(e)}}},{body:a.Object({file:a.File()}),response:a.Object({ok:a.Boolean(),filename:a.Optional(a.String()),error:a.Optional(a.String())})}).get(`/buttress/download/:filename`,async({params:{filename:e},store:{config:t},status:n})=>{let i=_.join(t.server.temp_file_dir,e);return _.relative(t.server.temp_file_dir,i).includes(`..`)?(n(400),`Invalid file path`):r(i)},{params:a.Object({filename:a.String()})});const ai=_.dirname(D(import.meta.url)),oi=async()=>{let e=[_.join(ai,`..`,`public`,`status.html`),_.join(ai,`..`,`..`,`public`,`status.html`)];return(await Promise.all(e.map(e=>c.access(e).then(()=>e,()=>null)))).find(e=>e!==null)??null},si=e=>{let{status:t}=e;return t?.getFullStatus?t.getFullStatus():{timestamp:new Date().toISOString(),ggmlLlm:{generators:[],history:{}},ggmlStt:{generators:[],history:{}}}},ci=async()=>{let e=await oi();if(!e)return console.error(`[Status] Failed to find status.html in candidate paths`),new Response(`Status page not found`,{status:404,headers:{"Content-Type":`text/plain`}});try{let t=await c.readFile(e,`utf-8`);return new Response(t,{headers:{"Content-Type":`text/html; charset=utf-8`}})}catch(e){return console.error(`[Status] Failed to serve status page:`,e),new Response(`Status page not found`,{status:404,headers:{"Content-Type":`text/plain`}})}};var li=$r().get(`/status`,ci).get(`/status/`,ci).get(`/buttress/status`,({store:{backend:e}})=>si(e));const ui=[`ggml-llm`,`mlx-llm`],di=new Map;function fi(e,t){return t===`mlx-llm`?e.mlxLlm:e.ggmlLlm}async function pi(e,t,n,r=`[LLM]`){let i=(t.generators||[]).filter(e=>ui.includes(e.type));if(i.length===0)throw Error(`No LLM generator configured. Add a [[generators]] with type = "ggml-llm" or "mlx-llm" to your config.`);let a=i[0],o=n||a.model?.repo_id;if(n){let e=i.find(e=>e.model?.repo_id===n);e&&(a=e)}else o=a.model?.repo_id;let s=a.type||`ggml-llm`,c=o,l=di.get(c);if(l?.initialized)return l;let{generators:u,server:d,...f}=t.global||{},p={...f,...a,model:{...a.model,repo_id:o}};console.log(`${r} Creating ${s} generator for ${c}`);let{id:m}=await e.startGenerator(s,p),h={id:m,type:s,config:p,repoId:o,initialized:!1};return di.set(c,h),await fi(e,s).initContext(m,{}),h.initialized=!0,console.log(`${r} Generator ready: ${c}`),h}function mi(e){let t=e.timings||{},n=t.prompt_n??t.promptN??0,r=t.cache_n??t.cacheN??0,i=t.predicted_n??t.predictedN??0;return{promptTokens:n||e.prompt_tokens||e.promptTokens||0,cachedTokens:r,completionTokens:i||e.tokens_evaluated||e.tokensEvaluated||e.tokens_predicted||e.tokensPredicted||0}}function hi(e){let{promptTokens:t,cachedTokens:n,completionTokens:r}=mi(e),i=t+n;return{prompt_tokens:i,completion_tokens:r,total_tokens:i+r}}const gi=()=>`chatcmpl-${Date.now()}-${Math.random().toString(36).slice(2,9)}`;async function _i(e,t,n,r){let i=e.getReader(),a=``,o=null,s=null,c=`stop`,l={prompt_tokens:0,completion_tokens:0,total_tokens:0};try{let e=!1;for(;!e;){let n=await i.read();if({done:e}=n,e)break;let{event:r,data:u}=n.value;if(r===`token`)u.content!=null&&(a=u.content),u.reasoning_content!=null&&(o=u.reasoning_content);else if(r===`result`)u.content==null?u.text&&(a=u.text):a=u.content,u.reasoning_content!=null&&(o=u.reasoning_content),u.tool_calls?.length>0?(s=u.tool_calls.map((e,n)=>({id:e.id||`call_${t}_${n}`,type:`function`,function:{name:e.function?.name||``,arguments:e.function?.arguments||``}})),c=`tool_calls`):c=u.interrupted?`length`:`stop`,l=hi(u);else if(r===`error`)throw Error(u.message)}}finally{i.cancel().catch(()=>{})}let u={role:`assistant`,content:a||null};return o&&(u.reasoning_content=o),s&&(u.tool_calls=s),{id:t,object:`chat.completion`,created:n,model:r,choices:[{index:0,message:u,finish_reason:c}],usage:l}}function vi({global:e}){let t=$r({prefix:`/oai-compat`});return t.use(M({origin:e?.openai_compat?.cors_allowed_origins??!1,methods:[`GET`,`POST`,`OPTIONS`],allowedHeaders:[`Content-Type`,`Authorization`],maxAge:86400,preflight:!0})),t.get(`/v1/models`,({store:e})=>{let{config:t}=e,n=(t.generators||[]).filter(e=>ui.includes(e.type)).map(e=>({id:e.model?.repo_id||e.type,object:`model`,created:Math.floor(Date.now()/1e3),owned_by:`local`}));return n.length===0&&n.push({id:`ggml-llm`,object:`model`,created:Math.floor(Date.now()/1e3),owned_by:`local`}),{object:`list`,data:n}}),t.post(`/v1/chat/completions`,async function*({body:e,set:t,store:n}){let{config:r,backend:a}=n,{messages:o=[],stream:s=!1,model:c,tools:l,temperature:u,stop:d,top_p:f,max_tokens:p,presence_penalty:m,frequency_penalty:h,tool_choice:g,stream_options:_,enable_thinking:v}=e;if(!o||o.length===0)return t.status=400,{error:{message:`messages is required and must not be empty`,type:`invalid_request_error`}};try{let e=await pi(a,r,c,`[OpenAI]`),t=gi(),n=Math.floor(Date.now()/1e3),y=e.repoId||`ggml-llm`,b={reasoning_format:`auto`,messages:o,jinja:!0,add_generation_prompt:!0};u!=null&&(b.temperature=u),f!=null&&(b.top_p=f),p!=null&&(b.n_predict=p),d!=null&&(b.stop=Array.isArray(d)?d:[d]),m!=null&&(b.presence_penalty=m),h!=null&&(b.frequency_penalty=h),l!=null&&(b.tools=l),g!=null&&(b.tool_choice=g),b.enable_thinking=v??!1;let x=await fi(a,e.type).completion(e.id,{options:b});if(!s)return await _i(x,t,n,y);let S=_?.include_usage===!0,C=x.getReader(),w=``,T=``,E=new Map,D=new Map;try{let e=!1;for(;!e;){let r=await C.read();if({done:e}=r,e)break;let{event:a,data:o}=r.value;if(a===`token`){let e={};if(o.content!=null){let t=o.content;t.length>w.length&&(e.content=t.slice(w.length),w=t)}if(o.reasoning_content!=null){let t=o.reasoning_content;t.length>T.length&&(e.reasoning_content=t.slice(T.length),T=t)}if(o.tool_calls?.length>0){let n=[];o.tool_calls.forEach((e,r)=>{let i={index:r};D.has(r)||(D.set(r,e.id||`call_${t}_${r}`),i.id=D.get(r),i.type=`function`);let a=e.function?.arguments||``,o=E.get(r)||``,s={};!E.has(r)&&e.function?.name&&(s.name=e.function.name),a.length>o.length&&(s.arguments=a.slice(o.length),E.set(r,a)),Object.keys(s).length>0?(i.function=s,n.push(i)):i.id&&(i.function={name:e.function?.name||``,arguments:``},n.push(i))}),n.length>0&&(e.tool_calls=n)}Object.keys(e).length>0&&(yield i({data:JSON.stringify({id:t,object:`chat.completion.chunk`,created:n,model:y,choices:[{index:0,delta:e,finish_reason:null}]})}))}else if(a===`result`){let e=`stop`;o.tool_calls?.length>0||D.size>0?e=`tool_calls`:o.interrupted&&(e=`length`);let r={id:t,object:`chat.completion.chunk`,created:n,model:y,choices:[{index:0,delta:{},finish_reason:e}]};S&&(r.usage=hi(o)),yield i({data:JSON.stringify(r)})}else a===`error`&&(yield i({data:JSON.stringify({error:{message:o.message,type:`server_error`}})}))}yield i({data:`[DONE]`})}finally{C.cancel().catch(()=>{})}}catch(e){return console.error(`[OpenAI] Chat completion error:`,e),t.status=500,{error:{message:e.message||`Internal server error`,type:`server_error`}}}},{body:a.Object({model:a.Optional(a.String()),messages:a.Array(a.Any()),stream:a.Optional(a.Boolean()),temperature:a.Optional(a.Number()),top_p:a.Optional(a.Number()),max_tokens:a.Optional(a.Number()),stop:a.Optional(a.Union([a.String(),a.Array(a.String())])),presence_penalty:a.Optional(a.Number()),frequency_penalty:a.Optional(a.Number()),tools:a.Optional(a.Array(a.Any())),tool_choice:a.Optional(a.Any()),stream_options:a.Optional(a.Object({include_usage:a.Optional(a.Boolean())})),enable_thinking:a.Optional(a.Boolean())})}),t}const yi=()=>`msg_${Date.now()}${Math.random().toString(36).slice(2,11)}`;function bi(e){let t={},n=[];if(e.system!=null){let t=``;if(typeof e.system==`string`)t=e.system;else if(Array.isArray(e.system))for(let n of e.system)n?.type===`text`&&typeof n.text==`string`&&(t+=n.text);t&&n.push({role:`system`,content:t})}if(!Array.isArray(e.messages))throw Error(`'messages' is required and must be an array`);for(let t of e.messages){let e=t?.role||`user`;if(t?.content==null){if(e===`assistant`)continue;n.push(t);continue}if(typeof t.content==`string`){n.push({role:e,content:t.content});continue}if(!Array.isArray(t.content)){n.push(t);continue}let r=[],i=[],a=[],o=``,s=!1;for(let e of t.content){let t=e?.type||``;if(t===`text`)i.push({type:`text`,text:e.text||``});else if(t===`thinking`)o+=e.thinking||``;else if(t===`image`){let t=e.source||{};if(t.type===`base64`){let e=t.media_type||`image/jpeg`,n=t.data||``;i.push({type:`image_url`,image_url:{url:`data:${e};base64,${n}`}})}else t.type===`url`&&i.push({type:`image_url`,image_url:{url:t.url||``}})}else if(t===`tool_use`)r.push({id:e.id||``,type:`function`,function:{name:e.name||``,arguments:JSON.stringify(e.input??{})}}),s=!0;else if(t===`tool_result`){let t=e.tool_use_id||``,n=``,r=e.content;if(typeof r==`string`)n=r;else if(Array.isArray(r))for(let e of r)e?.type===`text`&&(n+=e.text||``);a.push({role:`tool`,tool_call_id:t,content:n})}}if(i.length>0||s||o){let t={role:e};i.length>0?t.content=i:(s||o)&&(t.content=``),r.length>0&&(t.tool_calls=r),o&&(t.reasoning_content=o),n.push(t)}for(let e of a)n.push(e)}if(t.messages=n,Array.isArray(e.tools)&&(t.tools=e.tools.map(e=>({type:`function`,function:{name:e.name||``,description:e.description||``,parameters:e.input_schema||{}}}))),e.tool_choice&&typeof e.tool_choice==`object`){let n=e.tool_choice.type;n===`auto`?t.tool_choice=`auto`:n===`any`||n===`tool`?t.tool_choice=`required`:n===`none`&&(t.tool_choice=`none`)}else Array.isArray(t.tools)&&t.tools.length>0&&(t.tool_choice=`auto`);e.stop_sequences!=null&&(t.stop=Array.isArray(e.stop_sequences)?e.stop_sequences:[e.stop_sequences]),t.max_tokens=e.max_tokens??4096;for(let n of[`temperature`,`top_p`,`top_k`,`stream`])e[n]!=null&&(t[n]=e[n]);return e.thinking&&typeof e.thinking==`object`&&e.thinking.type===`enabled`&&(t.enable_thinking=!0,e.thinking.budget_tokens!=null&&(t.thinking_budget_tokens=e.thinking.budget_tokens)),t}function xi(e,t){return t?`tool_use`:e.stopping_word||e.stoppingWord?`stop_sequence`:e.interrupted||e.truncated?`max_tokens`:`end_turn`}function Si(e){let{promptTokens:t,cachedTokens:n,completionTokens:r}=mi(e);return{cache_read_input_tokens:n,input_tokens:t,output_tokens:r}}async function Ci(e,t,n){let r=e.getReader(),i=``,a=``,o=[],s={cache_read_input_tokens:0,input_tokens:0,output_tokens:0},c=`end_turn`,l=null;try{let e=!1;for(;!e;){let t=await r.read();if({done:e}=t,e)break;let{event:n,data:u}=t.value;if(n===`token`)u.content!=null&&(i=u.content),u.reasoning_content!=null&&(a=u.reasoning_content);else if(n===`result`)u.content==null?u.text&&u.reasoning_content==null&&(i=u.text):i=u.content,u.reasoning_content!=null&&(a=u.reasoning_content),Array.isArray(u.tool_calls)&&(o=u.tool_calls),s=Si(u),l=u.stopping_word||u.stoppingWord||null,c=xi(u,o.length>0);else if(n===`error`)throw Error(u.message||`completion error`)}}finally{r.cancel().catch(()=>{})}let u=[];a&&u.push({type:`thinking`,thinking:a,signature:``}),i&&u.push({type:`text`,text:i});for(let e of o){let t={};try{t=JSON.parse(e.function?.arguments||`{}`)}catch{t={}}u.push({type:`tool_use`,id:e.id||`toolu_${Math.random().toString(36).slice(2,11)}`,name:e.function?.name||``,input:t})}return{id:t,type:`message`,role:`assistant`,content:u,model:n,stop_reason:c,stop_sequence:l,usage:s}}function wi({global:e}){let t=$r({prefix:`/anthropic-messages`});return t.use(M({origin:e?.anthropic_messages?.cors_allowed_origins??!1,methods:[`GET`,`POST`,`OPTIONS`],allowedHeaders:[`Content-Type`,`Authorization`,`x-api-key`,`anthropic-version`],maxAge:86400,preflight:!0})),t.post(`/v1/messages`,async function*({body:e,set:t,store:n}){let{config:r,backend:a}=n,o=e;try{if(!Array.isArray(o.messages)||o.messages.length===0)return t.status=400,{type:`error`,error:{type:`invalid_request_error`,message:`messages is required and must not be empty`}};let e=bi(o),n=await pi(a,r,o.model,`[Anthropic]`),s=yi(),c=n.repoId||`ggml-llm`,l={reasoning_format:`auto`,messages:e.messages,jinja:!0,add_generation_prompt:!0};e.temperature!=null&&(l.temperature=e.temperature),e.top_p!=null&&(l.top_p=e.top_p),e.top_k!=null&&(l.top_k=e.top_k),e.max_tokens!=null&&(l.n_predict=e.max_tokens),e.stop!=null&&(l.stop=e.stop),e.tools!=null&&(l.tools=e.tools),e.tool_choice!=null&&(l.tool_choice=e.tool_choice),l.enable_thinking=e.enable_thinking??!1,e.thinking_budget_tokens!=null&&(l.thinking_budget_tokens=e.thinking_budget_tokens);let u=await fi(a,n.type).completion(n.id,{options:l});if(!o.stream)return await Ci(u,s,c);let d=u.getReader(),f=``,p=``,m=new Map,h=new Map,g=new Map,_=new Set,v=!1,y=!1,b=0,x=0,S={cache_read_input_tokens:0,input_tokens:0,output_tokens:0},C=`end_turn`,w=null,T=!1,E=e=>(v?1:0)+(y?1:0)+e;try{let e=!1;for(;!e;){let t=await d.read();if({done:e}=t,e)break;let{event:n,data:r}=t.value;if(n===`token`){if(!T){let e=Si(r);yield i({event:`message_start`,data:JSON.stringify({type:`message_start`,message:{id:s,type:`message`,role:`assistant`,content:[],model:c,stop_reason:null,stop_sequence:null,usage:e}})}),T=!0}if(r.reasoning_content!=null){let e=r.reasoning_content;e.length>p.length&&(v||(yield i({event:`content_block_start`,data:JSON.stringify({type:`content_block_start`,index:0,content_block:{type:`thinking`,thinking:``}})}),v=!0,b=1),yield i({event:`content_block_delta`,data:JSON.stringify({type:`content_block_delta`,index:0,delta:{type:`thinking_delta`,thinking:e.slice(p.length)}})}),p=e)}if(r.content!=null){let e=r.content;e.length>f.length&&(y||=(yield i({event:`content_block_start`,data:JSON.stringify({type:`content_block_start`,index:b,content_block:{type:`text`,text:``}})}),!0),yield i({event:`content_block_delta`,data:JSON.stringify({type:`content_block_delta`,index:b,delta:{type:`text_delta`,text:e.slice(f.length)}})}),f=e)}if(Array.isArray(r.tool_calls)&&r.tool_calls.length>0){for(let e=0;e<r.tool_calls.length;e+=1){let t=r.tool_calls[e],n=E(e),a=t?.function?.arguments||``,o=m.get(e)||``;if(!_.has(e)){let r=t?.id||`toolu_${s}_${e}`,a=t?.function?.name||g.get(e)||``;h.set(e,r),g.set(e,a),_.add(e),yield i({event:`content_block_start`,data:JSON.stringify({type:`content_block_start`,index:n,content_block:{type:`tool_use`,id:r,name:a,input:{}}})})}a.length>o.length&&(yield i({event:`content_block_delta`,data:JSON.stringify({type:`content_block_delta`,index:n,delta:{type:`input_json_delta`,partial_json:a.slice(o.length)}})}),m.set(e,a))}x=r.tool_calls.length}}else if(n===`result`){if(!T){let e=Si(r);yield i({event:`message_start`,data:JSON.stringify({type:`message_start`,message:{id:s,type:`message`,role:`assistant`,content:[],model:c,stop_reason:null,stop_sequence:null,usage:e}})}),T=!0}Array.isArray(r.tool_calls)&&(x=Math.max(x,r.tool_calls.length)),S=Si(r),w=r.stopping_word||r.stoppingWord||null,C=xi(r,_.size>0)}else if(n===`error`){yield i({event:`error`,data:JSON.stringify({type:`error`,error:{type:`api_error`,message:r.message||`completion error`}})});return}}v&&(yield i({event:`content_block_delta`,data:JSON.stringify({type:`content_block_delta`,index:0,delta:{type:`signature_delta`,signature:``}})}),yield i({event:`content_block_stop`,data:JSON.stringify({type:`content_block_stop`,index:0})})),y&&(yield i({event:`content_block_stop`,data:JSON.stringify({type:`content_block_stop`,index:b})}));for(let e of[..._].sort((e,t)=>e-t))yield i({event:`content_block_stop`,data:JSON.stringify({type:`content_block_stop`,index:E(e)})});yield i({event:`message_delta`,data:JSON.stringify({type:`message_delta`,delta:{stop_reason:C,stop_sequence:w},usage:{output_tokens:S.output_tokens}})}),yield i({event:`message_stop`,data:JSON.stringify({type:`message_stop`})})}finally{d.cancel().catch(()=>{})}}catch(e){return console.error(`[Anthropic] Messages error:`,e),t.status=500,{type:`error`,error:{type:`api_error`,message:e?.message||`Internal server error`}}}},{body:a.Object({model:a.Optional(a.String()),messages:a.Array(a.Any()),system:a.Optional(a.Any()),max_tokens:a.Optional(a.Number()),stream:a.Optional(a.Boolean()),temperature:a.Optional(a.Number()),top_p:a.Optional(a.Number()),top_k:a.Optional(a.Number()),stop_sequences:a.Optional(a.Union([a.String(),a.Array(a.String())])),tools:a.Optional(a.Array(a.Any())),tool_choice:a.Optional(a.Any()),thinking:a.Optional(a.Any()),metadata:a.Optional(a.Any())})}),t.post(`/v1/messages/count_tokens`,async({body:e,set:t,store:n})=>{let{config:r,backend:i}=n,a=e;try{let e=bi(a),t=await pi(i,r,a.model,`[Anthropic]`),n=fi(i,t.type),o={messages:e.messages,add_generation_prompt:!0,jinja:!0};e.tools!=null&&(o.tools=e.tools);let s=await n.applyChatTemplate(t.id,o),c=typeof s==`string`?s:s?.prompt||``,l=await n.tokenize(t.id,{text:c,add_special:!0,parse_special:!0});return{input_tokens:(Array.isArray(l)?l:l?.tokens||[]).length}}catch(e){return console.error(`[Anthropic] count_tokens error:`,e),t.status=500,{type:`error`,error:{type:`api_error`,message:e?.message||`Internal server error`}}}},{body:a.Object({model:a.Optional(a.String()),messages:a.Array(a.Any()),system:a.Optional(a.Any()),tools:a.Optional(a.Array(a.Any())),tool_choice:a.Optional(a.Any())})}),t.get(`/v1/models`,({store:e})=>{let{config:t}=e,n=(t.generators||[]).filter(e=>ui.includes(e.type)).map(e=>{let t=e.model?.repo_id||e.type;return{id:t,type:`model`,display_name:t,created_at:new Date().toISOString()}});return n.length===0&&n.push({id:`ggml-llm`,type:`model`,display_name:`ggml-llm`,created_at:new Date().toISOString()}),{data:n,has_more:!1,first_id:n[0]?.id,last_id:n.at(-1)?.id}}),t}const Ti=(e={},t={})=>{let n=Array.isArray(e)?[...e]:{...e};return Object.entries(t||{}).forEach(([e,t])=>{t&&typeof t==`object`&&!Array.isArray(t)?n[e]=Ti(n[e]||{},t):n[e]=t}),n},Ei=e=>e&&typeof e==`object`?structuredClone(e):null,Di=(e,t)=>Ti(Ei(e)||{},Ei(t)||{}),Oi=(e,t)=>Ti(structuredClone(e.global),t||{}),ki=(e,t,n,r)=>{if(e.generators.length>0){let i=e.generators.filter(e=>e?.type===n);if(i.length>0&&r){let a=i.find(e=>t.getModelIdentifier(n,e)===r);if(a)return Oi(e,a)}}return Object.keys(e.global).length>0?Oi(e,{}):null},Ai={udp:{port:8089,announcements:{enabled:!0,interval:5e3},requests:{enabled:!0,responseDelay:100}},http:{enabled:!0,path:`/buttress/info`,cors:!0}},ji=e=>e?e===!0?{...Ai}:Ti(Ai,e):null,Mi=(e,t)=>{if(!e.generators||e.generators.length===0)return t.map(e=>({type:e}));let n=new Set;return e.generators.forEach(e=>{e.type&&n.add(e.type)}),n.size===0?t.map(e=>({type:e})):Array.from(n).map(e=>({type:e}))},Ni=(e,t,n)=>e===void 0?n:typeof e==`number`?e:t(e)??n,Pi=6e4,Fi=1024*1024*50,Ii=e=>{let t=N.machineIdSync(),n=Ti({server:{id:`buttress-${t}`,name:`Buttress Server (${t.slice(-8)})`,port:2080,temp_file_dir:_.join(v.tmpdir(),`.buttress`),session_timeout:Pi,max_body_size:Fi},autodiscover:!1},Ei(e)||{}),r=Array.isArray(n.generators)?n.generators:[],{server:i,generators:a,autodiscover:o,...s}=n;return{autodiscover:ji(o),server:{id:i.id,name:i.name,port:i.port,log_level:i.log_level,temp_file_dir:i.temp_file_dir,max_body_size:Ni(i.max_body_size,w.parse,Fi),session_timeout:Ni(i.session_timeout,P,Pi)},global:s,generators:r}},Li={getCapabilities:j.tuple([j.object({type:j.string().optional().default(`ggml-llm`),config:j.any().optional(),currentClientCapabilities:j.any().optional(),options:j.any().optional()}).nullable().optional()]),startGenerator:j.tuple([j.string(),j.any().optional()]),finalizeGenerator:j.tuple([j.string()])};var Ri={async getCapabilities({backend:e,config:t},n=null){console.log(`[Server] Get Capabilities:`,n);let{type:r=`ggml-llm`,config:i,currentClientCapabilities:a=null,options:o={}}=n||{type:`ggml-llm`},s=Ei(i),c=Di(ki(t,e,r,e.getModelIdentifier(r,s)),i);if(Object.keys(c).length===0)throw Error(`Buttress server missing generator configuration`);return c.backend=c.backend||{},c.backend.type||(c.backend.type=r),e.getCapabilities(r,a,{...o,config:c})},async startGenerator({backend:e,config:t,session:n},r,i){console.log(`[Server] Start Generator:`,r,i);let a=Ei(i),o=Di(ki(t,e,r,e.getModelIdentifier(r,a)),i);if(Object.keys(o).length===0)throw Error(`Buttress server missing generator configuration`);o.backend=o.backend||{},o.backend.type||(o.backend.type=r);let s=await e.startGenerator(r,o);return n.generators.add(s.id),s},async finalizeGenerator({backend:e,session:t},n){return console.log(`[Server] Finalize Generator:`,n),t.generators.delete(n),e.finalizeGenerator(n)}};const zi={initContext:j.tuple([j.string(),j.any().optional()]),completion:j.tuple([j.string(),j.any().optional()]),tokenize:j.tuple([j.string(),j.any()]),detokenize:j.tuple([j.string(),j.any()]),applyChatTemplate:j.tuple([j.string(),j.any()]),releaseContext:j.tuple([j.string()])};function Bi(e){return function({backend:t,session:n},r,i){return new s({async start(a){try{let o=await e(t).initContext(r,{...i,onProgress:e=>{a.enqueue({event:`progress`,data:{progress:e}})}});n.initializedContexts.add(r),await new Promise(e=>setTimeout(e,1e3));let{download:s,...c}=o||{};a.enqueue({event:`result`,data:{result:c}}),a.close()}catch(e){a.error(e)}}})}}function Vi(e,t){return async function({backend:n,session:r},i,a){return console.log(`[Server] ${t}:`,{id:i,force:a}),r.initializedContexts.has(i)?(r.initializedContexts.delete(i),e(n).releaseContext(i,{force:a})):(console.log(`[Server] ${t} skipped - not initialized by this session:`,{id:i}),{released:!1,skipped:!0})}}function Hi(e,t){return{initContext:Bi(e),completion({backend:n},r,i){return console.log(`[Server] ${t}Completion:`,{id:r,property:i}),e(n).completion(r,i)},async tokenize({backend:n},r,i){return console.log(`[Server] ${t}Tokenize:`,{id:r,property:i}),e(n).tokenize(r,i)},async detokenize({backend:n},r,i){return console.log(`[Server] ${t}Detokenize:`,{id:r,property:i}),e(n).detokenize(r,i)},async applyChatTemplate({backend:n},r,i){return console.log(`[Server] ${t}Apply Chat Template:`,{id:r,property:i}),e(n).applyChatTemplate(r,i)},releaseContext:Vi(e,`${t}Release Context`)}}var Ui=Hi(e=>e.ggmlLlm,``);const Wi={initContext:j.tuple([j.string(),j.any().optional()]),transcribe:j.tuple([j.string(),j.string(),j.any().optional()]),transcribeData:j.tuple([j.string(),j.union([j.instanceof(Buffer),j.instanceof(Uint8Array)]),j.any().optional()]),releaseContext:j.tuple([j.string()])},Gi=e=>e.ggmlStt,Ki={common:Ri,ggmlLlm:Ui,ggmlStt:{initContext:Bi(Gi),async transcribe({backend:e,config:{server:t}},n,r,i){return console.log(`[Server] Transcribe:`,{id:n,audioPath:r,options:i}),e.ggmlStt.transcribe(n,{audioPath:_.join(t.temp_file_dir,r),options:i})},async transcribeData({backend:e},t,n,r){return console.log(`[Server] Transcribe Data:`,{id:t,audioDataLength:n?.length||0,options:r}),e.ggmlStt.transcribeData(t,{audioData:n,options:r})},releaseContext:Vi(Gi,`Release STT Context`)},mlxLlm:Hi(e=>e.mlxLlm,`MLX `)},qi={common:Li,ggmlLlm:zi,ggmlStt:Wi,mlxLlm:zi};var Ji=Ki;const Yi=e=>{try{return JSON.parse(e,(e,t)=>{if(!t)return t;if(t?.type===`Buffer`&&t?.data)return F.from(t.data,`base64`);if(t?.type===`Uint8Array`&&t?.data){let e=F.from(t.data,`base64`);return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}return t?.type===`Error`&&t?.name&&t?.message?Error(t.name,t.message):t})}catch{return e}},Xi=e=>{try{return JSON.stringify(e,(e,t)=>t instanceof Error?{type:`Error`,name:t.name,message:t.message}:t instanceof F?{type:`Buffer`,data:t.toString(`base64`)}:t instanceof Uint8Array?{type:`Uint8Array`,data:F.from(t).toString(`base64`)}:t)}catch{return e}};var Zi=class{name=`udp`;socket=null;announcementTimer=null;config;getServerInfo;port;constructor(e,t){this.config=e,this.getServerInfo=t,this.port=e.port??8089}async start(){if(this.socket=re.createSocket({type:`udp4`,reuseAddr:!0}),this.socket.on(`message`,(e,t)=>{this.handleMessage(e,t)}),this.socket.on(`error`,e=>{console.error(`[Autodiscover UDP] Socket error:`,e.message)}),await new Promise((e,t)=>{this.socket.bind(this.port,()=>{this.socket.setBroadcast(!0),console.log(`[Autodiscover UDP] Listening on port ${this.port}`),e()}),this.socket.once(`error`,t)}),this.config.announcements.enabled){let e=this.config.announcements.interval??5e3;this.announcementTimer=setInterval(()=>{this.sendAnnouncement()},e),this.sendAnnouncement()}}async stop(){this.announcementTimer&&=(clearInterval(this.announcementTimer),null),this.socket&&=(await new Promise(e=>{this.socket.close(()=>e())}),null)}handleMessage(e,t){try{let n=JSON.parse(e.toString());if(n.t===`QUERY`&&this.config.requests.enabled){let e=n.d,r=this.config.requests.responseDelay??0,i=r>0?Math.random()*r:0;setTimeout(()=>{this.sendResponse(e.id,t)},i)}}catch{}}sendAnnouncement(){if(!this.socket)return;let e={t:`ANNOUNCE`,v:`1.0`,d:{info:this.getServerInfo()}},t=Buffer.from(JSON.stringify(e));this.socket.send(t,0,t.length,this.port,`255.255.255.255`,e=>{e&&console.error(`[Autodiscover UDP] Announcement error:`,e.message)})}sendResponse(e,t){if(!this.socket)return;let n={t:`RESPONSE`,v:`1.0`,d:{request_id:e,info:this.getServerInfo()}},r=Buffer.from(JSON.stringify(n));this.socket.send(r,0,r.length,t.port,t.address,e=>{e&&console.error(`[Autodiscover UDP] Response error:`,e.message)})}},Qi=class{transports=[];started=!1;constructor(e,t){this.config=e,this.getServerInfo=t,(e.udp?.announcements?.enabled||e.udp?.requests?.enabled)&&this.transports.push(new Zi(e.udp,t))}async start(){this.started||=((await Promise.allSettled(this.transports.map(e=>e.start()))).forEach((e,t)=>{e.status===`rejected`&&console.error(`[Autodiscover] Failed to start ${this.transports[t].name}:`,e.reason)}),!0)}async stop(){this.started&&=(await Promise.allSettled(this.transports.map(e=>e.stop())),!1)}};const $i=()=>{let e=v.networkInterfaces();return Object.values(e).flat().find(e=>e?.family===`IPv4`&&!e?.internal)?.address||null},$=Zr(),ea=e=>{if(!e)return{repoId:null,filename:null};let[t,n]=e.split(`:`);return{repoId:t,filename:n||null}};async function ta({modelIds:e=[],defaultConfig:t=null}={}){let n=[];console.log(`${$.name} v${$.version}`),console.log(`Generating model capabilities comparison...
|
|
36
|
+
=== Full Capabilities JSON ===`),console.log(JSON.stringify(u,null,2)),process.exit(0)}catch(e){console.error(`Failed to get capabilities:`,e.message),process.exit(1)}}var Lr=e({finalizeGenerator:()=>Vr,generatorRegistry:()=>Z,getCapabilities:()=>J,getModelIdentifier:()=>Gr,ggmlLlm:()=>Hr,ggmlStt:()=>Wr,globalDownloadManager:()=>Rr,mlxLlm:()=>Ur,showModelsTable:()=>Nr,showSttModelsTable:()=>Fr,startGenerator:()=>Br,startModelDownload:()=>qr,status:()=>Kr,testGgmlLlmCapabilities:()=>Pr,testGgmlSttCapabilities:()=>Ir});const Z=new Map,Rr={downloads:new Map,getDownload(e){return this.downloads.get(e)||null},setDownload(e,t){this.downloads.set(e,t)},deleteDownload(e){this.downloads.delete(e)},isDownloading(e){return this.downloads.has(e)},getActiveDownloads(){return Array.from(this.downloads.entries()).map(([e,t])=>({localPath:e,promise:t}))}},zr=e=>{let t=Z.get(e);if(!t)throw Error(`Unknown generator id "${e}"`);return t},Q=(e,t)=>{let n=zr(e);if(n.type!==t)throw Error(`Generator "${e}" does not support ${t} backend`);return n.instance};async function Br(e,t){let n={"ggml-llm":{create:nn,getId:rn},"ggml-stt":{create:Gn,getId:Kn},"mlx-llm":{create:Dr,getId:Or}}[e];if(!n)throw Error(`Unsupported backend type: ${e}`);let r=n.getId(t);if(!r)throw Error(`Buttress generator config missing repo identifier`);let i=`${e}:${r}`,a=Z.get(i);if(a)return a.refCount+=1,a.instance.resetFinalized?.(),{id:a.id,info:a.instance.info};let o=await n.create(i,t,{globalDownloadManager:Rr}),s={id:i,type:o.type,instance:o,refCount:1};return Z.set(i,s),{id:i,info:o.info}}async function Vr(e){let t=Z.get(e);return t?(--t.refCount,t.refCount<=0&&(await t.instance.finalize(),(t.instance.hasPendingReleases?.()??!1)||Z.delete(e)),!0):!1}const Hr={async initContext(e,t){return Q(e,`ggml-llm`).initContext(t)},async completion(e,t){return Q(e,`ggml-llm`).completion(t)},async tokenize(e,t){return Q(e,`ggml-llm`).tokenize(t)},async detokenize(e,t){return Q(e,`ggml-llm`).detokenize(t)},async applyChatTemplate(e,t){return Q(e,`ggml-llm`).applyChatTemplate(t)},async releaseContext(e,t){let n=Z.get(e);if(!n)return{released:!0,alreadyReleased:!0};if(n.type!==`ggml-llm`)throw Error(`Generator "${e}" does not support ggml-llm backend`);return n.instance.releaseContext(t)}},Ur={async initContext(e,t){return Q(e,`mlx-llm`).initContext(t)},async completion(e,t){return Q(e,`mlx-llm`).completion(t)},async tokenize(e,t){return Q(e,`mlx-llm`).tokenize(t)},async detokenize(e,t){return Q(e,`mlx-llm`).detokenize(t)},async applyChatTemplate(e,t){return Q(e,`mlx-llm`).applyChatTemplate(t)},async releaseContext(e,t){let n=Z.get(e);if(!n)return{released:!0,alreadyReleased:!0};if(n.type!==`mlx-llm`)throw Error(`Generator "${e}" does not support mlx-llm backend`);return n.instance.releaseContext(t)}},Wr={async initContext(e,t){return Q(e,`ggml-stt`).initContext(t)},async transcribe(e,t){return Q(e,`ggml-stt`).transcribe(t)},async transcribeData(e,t){return Q(e,`ggml-stt`).transcribeData(t)},async releaseContext(e,t){let n=Z.get(e);if(!n)return{released:!0,alreadyReleased:!0};if(n.type!==`ggml-stt`)throw Error(`Generator "${e}" does not support ggml-stt backend`);return n.instance.releaseContext(t)}};function Gr(e,t){return e===`ggml-llm`?rn(t):e===`ggml-stt`?Kn(t):e===`mlx-llm`?Or(t):null}const Kr={getFullStatus:()=>et(Z),getGgmlLlmStatus:()=>Ze(Z),getGgmlSttStatus:()=>Qe(Z),getMlxLlmStatus:()=>$e(Z),subscribeToStatus:Ye,subscribeToStatusWithId:Xe,llmStatusTracker:H,sttStatusTracker:U,statusEmitter:V};async function qr(e,t,n={}){let r={"ggml-llm":an,"ggml-stt":qn,"mlx-llm":Ar}[e];return r?r(t,Rr,n):{started:!1,localPath:null,repoId:null,error:`Unknown backend type: ${e}`}}var Jr=`@fugood/buttress-server`,Yr=`2.25.0-beta.6`,Xr={name:Jr,version:Yr,main:`lib/index.mjs`,types:`lib/index.d.mts`,type:`module`,bin:{"bricks-buttress":`./bin/bricks-buttress`},files:[`lib`,`bin`,`config`,`public`],scripts:{typecheck:`tsc --noEmit`,build:`tsdown -c rolldown.config.js`,prepublish:`bun run build`,dev:`bun src/index.ts`,start:`bun lib/index.mjs`,"start-with-node":`node lib/index.mjs`},keywords:[`BRICKS`,`buttress`,`server`],license:`MIT`,dependencies:{"@elysiajs/cors":`^1.1.1`,"@elysiajs/node":`^1.4.2`,"@fugood/llama.node":`^1.7.0-rc.11`,"@fugood/whisper.node":`^1.0.18`,"@huggingface/gguf":`^0.3.2`,"@iarna/toml":`^3.0.0`,bytes:`^3.1.0`,elysia:`^1.4.19`,ms:`^2.1.1`,"node-machine-id":`^1.1.12`,zod:`^3.25.76`},devDependencies:{tsdown:`^0.20.1`,typescript:`^5.9.3`},gitHead:`d8a3bbd695e9118cf725b7226866d804bbe56bbe`};const Zr=()=>({version:Yr,name:Jr,description:Xr.description}),Qr=typeof process<`u`&&process.versions&&process.versions.node,$r=e=>new n({adapter:Qr?t():void 0,...e}),ei=a.Object({id:a.String(),name:a.String(),version:a.String(),generators:a.Array(a.Object({type:a.String()})),authentication:a.Object({required:a.Boolean(),type:a.Literal(`device-group`)})}),ti=({store:{serverInfo:e}})=>({id:e.id,name:e.name,version:e.version,generators:e.generators,authentication:e.authentication});var ni=e=>{let t=$r(),n=e.autodiscover.http?.path??`/buttress/info`;return t.get(n,ti,{response:ei}),t};const ri=typeof process<`u`&&process.versions!=null&&process.versions.node!=null;var ii=$r().post(`/buttress/upload`,async({body:{file:e},store:{config:t}})=>{let n=`${Date.now()}-${e.name.replace(/[^\dA-Za-z]/g,`_`)}`,r=_.join(t.server.temp_file_dir,n);try{return ri?await g(r,await e.stream()):await g(r,await e.arrayBuffer()),{ok:!0,filename:n}}catch(e){return{ok:!1,error:String(e)}}},{body:a.Object({file:a.File()}),response:a.Object({ok:a.Boolean(),filename:a.Optional(a.String()),error:a.Optional(a.String())})}).get(`/buttress/download/:filename`,async({params:{filename:e},store:{config:t},status:n})=>{let i=_.join(t.server.temp_file_dir,e);return _.relative(t.server.temp_file_dir,i).includes(`..`)?(n(400),`Invalid file path`):r(i)},{params:a.Object({filename:a.String()})});const ai=_.dirname(D(import.meta.url)),oi=async()=>{let e=[_.join(ai,`..`,`public`,`status.html`),_.join(ai,`..`,`..`,`public`,`status.html`)];return(await Promise.all(e.map(e=>c.access(e).then(()=>e,()=>null)))).find(e=>e!==null)??null},si=e=>{let{status:t}=e;return t?.getFullStatus?t.getFullStatus():{timestamp:new Date().toISOString(),ggmlLlm:{generators:[],history:{}},ggmlStt:{generators:[],history:{}}}},ci=async()=>{let e=await oi();if(!e)return console.error(`[Status] Failed to find status.html in candidate paths`),new Response(`Status page not found`,{status:404,headers:{"Content-Type":`text/plain`}});try{let t=await c.readFile(e,`utf-8`);return new Response(t,{headers:{"Content-Type":`text/html; charset=utf-8`}})}catch(e){return console.error(`[Status] Failed to serve status page:`,e),new Response(`Status page not found`,{status:404,headers:{"Content-Type":`text/plain`}})}};var li=$r().get(`/status`,ci).get(`/status/`,ci).get(`/buttress/status`,({store:{backend:e}})=>si(e));const ui=[`ggml-llm`,`mlx-llm`],di=new Map;function fi(e,t){return t===`mlx-llm`?e.mlxLlm:e.ggmlLlm}async function pi(e,t,n,r=`[LLM]`){let i=(t.generators||[]).filter(e=>ui.includes(e.type));if(i.length===0)throw Error(`No LLM generator configured. Add a [[generators]] with type = "ggml-llm" or "mlx-llm" to your config.`);let a=i[0],o=n||a.model?.repo_id;if(n){let e=i.find(e=>e.model?.repo_id===n);e&&(a=e)}else o=a.model?.repo_id;let s=a.type||`ggml-llm`,c=o,l=di.get(c);if(l?.initialized)return l;let{generators:u,server:d,...f}=t.global||{},p={...f,...a,model:{...a.model,repo_id:o}};console.log(`${r} Creating ${s} generator for ${c}`);let{id:m}=await e.startGenerator(s,p),h={id:m,type:s,config:p,repoId:o,initialized:!1};return di.set(c,h),await fi(e,s).initContext(m,{}),h.initialized=!0,console.log(`${r} Generator ready: ${c}`),h}function mi(e){let t=e.timings||{},n=t.prompt_n??t.promptN??0,r=t.cache_n??t.cacheN??0,i=t.predicted_n??t.predictedN??0;return{promptTokens:n||e.prompt_tokens||e.promptTokens||0,cachedTokens:r,completionTokens:i||e.tokens_evaluated||e.tokensEvaluated||e.tokens_predicted||e.tokensPredicted||0}}function hi(e){let{promptTokens:t,cachedTokens:n,completionTokens:r}=mi(e),i=t+n;return{prompt_tokens:i,completion_tokens:r,total_tokens:i+r}}const gi=()=>`chatcmpl-${Date.now()}-${Math.random().toString(36).slice(2,9)}`;async function _i(e,t,n,r){let i=e.getReader(),a=``,o=null,s=null,c=`stop`,l={prompt_tokens:0,completion_tokens:0,total_tokens:0};try{let e=!1;for(;!e;){let n=await i.read();if({done:e}=n,e)break;let{event:r,data:u}=n.value;if(r===`token`)u.content!=null&&(a=u.content),u.reasoning_content!=null&&(o=u.reasoning_content);else if(r===`result`)u.content==null?u.text&&(a=u.text):a=u.content,u.reasoning_content!=null&&(o=u.reasoning_content),u.tool_calls?.length>0?(s=u.tool_calls.map((e,n)=>({id:e.id||`call_${t}_${n}`,type:`function`,function:{name:e.function?.name||``,arguments:e.function?.arguments||``}})),c=`tool_calls`):c=u.interrupted?`length`:`stop`,l=hi(u);else if(r===`error`)throw Error(u.message)}}finally{i.cancel().catch(()=>{})}let u={role:`assistant`,content:a||null};return o&&(u.reasoning_content=o),s&&(u.tool_calls=s),{id:t,object:`chat.completion`,created:n,model:r,choices:[{index:0,message:u,finish_reason:c}],usage:l}}function vi({global:e}){let t=$r({prefix:`/oai-compat`});return t.use(M({origin:e?.openai_compat?.cors_allowed_origins??!1,methods:[`GET`,`POST`,`OPTIONS`],allowedHeaders:[`Content-Type`,`Authorization`],maxAge:86400,preflight:!0})),t.get(`/v1/models`,({store:e})=>{let{config:t}=e,n=(t.generators||[]).filter(e=>ui.includes(e.type)).map(e=>({id:e.model?.repo_id||e.type,object:`model`,created:Math.floor(Date.now()/1e3),owned_by:`local`}));return n.length===0&&n.push({id:`ggml-llm`,object:`model`,created:Math.floor(Date.now()/1e3),owned_by:`local`}),{object:`list`,data:n}}),t.post(`/v1/chat/completions`,async function*({body:e,set:t,store:n}){let{config:r,backend:a}=n,{messages:o=[],stream:s=!1,model:c,tools:l,temperature:u,stop:d,top_p:f,max_tokens:p,presence_penalty:m,frequency_penalty:h,tool_choice:g,stream_options:_,enable_thinking:v}=e;if(!o||o.length===0)return t.status=400,{error:{message:`messages is required and must not be empty`,type:`invalid_request_error`}};try{let e=await pi(a,r,c,`[OpenAI]`),t=gi(),n=Math.floor(Date.now()/1e3),y=e.repoId||`ggml-llm`,b={reasoning_format:`auto`,messages:o,jinja:!0,add_generation_prompt:!0};u!=null&&(b.temperature=u),f!=null&&(b.top_p=f),p!=null&&(b.n_predict=p),d!=null&&(b.stop=Array.isArray(d)?d:[d]),m!=null&&(b.presence_penalty=m),h!=null&&(b.frequency_penalty=h),l!=null&&(b.tools=l),g!=null&&(b.tool_choice=g),b.enable_thinking=v??!1;let x=await fi(a,e.type).completion(e.id,{options:b});if(!s)return await _i(x,t,n,y);let S=_?.include_usage===!0,C=x.getReader(),w=``,T=``,E=new Map,D=new Map;try{let e=!1;for(;!e;){let r=await C.read();if({done:e}=r,e)break;let{event:a,data:o}=r.value;if(a===`token`){let e={};if(o.content!=null){let t=o.content;t.length>w.length&&(e.content=t.slice(w.length),w=t)}if(o.reasoning_content!=null){let t=o.reasoning_content;t.length>T.length&&(e.reasoning_content=t.slice(T.length),T=t)}if(o.tool_calls?.length>0){let n=[];o.tool_calls.forEach((e,r)=>{let i={index:r};D.has(r)||(D.set(r,e.id||`call_${t}_${r}`),i.id=D.get(r),i.type=`function`);let a=e.function?.arguments||``,o=E.get(r)||``,s={};!E.has(r)&&e.function?.name&&(s.name=e.function.name),a.length>o.length&&(s.arguments=a.slice(o.length),E.set(r,a)),Object.keys(s).length>0?(i.function=s,n.push(i)):i.id&&(i.function={name:e.function?.name||``,arguments:``},n.push(i))}),n.length>0&&(e.tool_calls=n)}Object.keys(e).length>0&&(yield i({data:JSON.stringify({id:t,object:`chat.completion.chunk`,created:n,model:y,choices:[{index:0,delta:e,finish_reason:null}]})}))}else if(a===`result`){let e=`stop`;o.tool_calls?.length>0||D.size>0?e=`tool_calls`:o.interrupted&&(e=`length`);let r={id:t,object:`chat.completion.chunk`,created:n,model:y,choices:[{index:0,delta:{},finish_reason:e}]};S&&(r.usage=hi(o)),yield i({data:JSON.stringify(r)})}else a===`error`&&(yield i({data:JSON.stringify({error:{message:o.message,type:`server_error`}})}))}yield i({data:`[DONE]`})}finally{C.cancel().catch(()=>{})}}catch(e){return console.error(`[OpenAI] Chat completion error:`,e),t.status=500,{error:{message:e.message||`Internal server error`,type:`server_error`}}}},{body:a.Object({model:a.Optional(a.String()),messages:a.Array(a.Any()),stream:a.Optional(a.Boolean()),temperature:a.Optional(a.Number()),top_p:a.Optional(a.Number()),max_tokens:a.Optional(a.Number()),stop:a.Optional(a.Union([a.String(),a.Array(a.String())])),presence_penalty:a.Optional(a.Number()),frequency_penalty:a.Optional(a.Number()),tools:a.Optional(a.Array(a.Any())),tool_choice:a.Optional(a.Any()),stream_options:a.Optional(a.Object({include_usage:a.Optional(a.Boolean())})),enable_thinking:a.Optional(a.Boolean())})}),t}const yi=()=>`msg_${Date.now()}${Math.random().toString(36).slice(2,11)}`;function bi(e){let t={},n=[];if(e.system!=null){let t=``;if(typeof e.system==`string`)t=e.system;else if(Array.isArray(e.system))for(let n of e.system)n?.type===`text`&&typeof n.text==`string`&&(t+=n.text);t&&n.push({role:`system`,content:t})}if(!Array.isArray(e.messages))throw Error(`'messages' is required and must be an array`);for(let t of e.messages){let e=t?.role||`user`;if(t?.content==null){if(e===`assistant`)continue;n.push(t);continue}if(typeof t.content==`string`){n.push({role:e,content:t.content});continue}if(!Array.isArray(t.content)){n.push(t);continue}let r=[],i=[],a=[],o=``,s=!1;for(let e of t.content){let t=e?.type||``;if(t===`text`)i.push({type:`text`,text:e.text||``});else if(t===`thinking`)o+=e.thinking||``;else if(t===`image`){let t=e.source||{};if(t.type===`base64`){let e=t.media_type||`image/jpeg`,n=t.data||``;i.push({type:`image_url`,image_url:{url:`data:${e};base64,${n}`}})}else t.type===`url`&&i.push({type:`image_url`,image_url:{url:t.url||``}})}else if(t===`tool_use`)r.push({id:e.id||``,type:`function`,function:{name:e.name||``,arguments:JSON.stringify(e.input??{})}}),s=!0;else if(t===`tool_result`){let t=e.tool_use_id||``,n=``,r=e.content;if(typeof r==`string`)n=r;else if(Array.isArray(r))for(let e of r)e?.type===`text`&&(n+=e.text||``);a.push({role:`tool`,tool_call_id:t,content:n})}}if(i.length>0||s||o){let t={role:e};i.length>0?t.content=i:(s||o)&&(t.content=``),r.length>0&&(t.tool_calls=r),o&&(t.reasoning_content=o),n.push(t)}for(let e of a)n.push(e)}if(t.messages=n,Array.isArray(e.tools)&&(t.tools=e.tools.map(e=>({type:`function`,function:{name:e.name||``,description:e.description||``,parameters:e.input_schema||{}}}))),e.tool_choice&&typeof e.tool_choice==`object`){let n=e.tool_choice.type;n===`auto`?t.tool_choice=`auto`:n===`any`||n===`tool`?t.tool_choice=`required`:n===`none`&&(t.tool_choice=`none`)}else Array.isArray(t.tools)&&t.tools.length>0&&(t.tool_choice=`auto`);e.stop_sequences!=null&&(t.stop=Array.isArray(e.stop_sequences)?e.stop_sequences:[e.stop_sequences]),t.max_tokens=e.max_tokens??4096;for(let n of[`temperature`,`top_p`,`top_k`,`stream`])e[n]!=null&&(t[n]=e[n]);return e.thinking&&typeof e.thinking==`object`&&e.thinking.type===`enabled`&&(t.enable_thinking=!0,e.thinking.budget_tokens!=null&&(t.thinking_budget_tokens=e.thinking.budget_tokens)),t}function xi(e,t){return t?`tool_use`:e.stopping_word||e.stoppingWord?`stop_sequence`:e.interrupted||e.truncated?`max_tokens`:`end_turn`}function Si(e){let{promptTokens:t,cachedTokens:n,completionTokens:r}=mi(e);return{cache_read_input_tokens:n,input_tokens:t,output_tokens:r}}async function Ci(e,t,n){let r=e.getReader(),i=``,a=``,o=[],s={cache_read_input_tokens:0,input_tokens:0,output_tokens:0},c=`end_turn`,l=null;try{let e=!1;for(;!e;){let t=await r.read();if({done:e}=t,e)break;let{event:n,data:u}=t.value;if(n===`token`)u.content!=null&&(i=u.content),u.reasoning_content!=null&&(a=u.reasoning_content);else if(n===`result`)u.content==null?u.text&&u.reasoning_content==null&&(i=u.text):i=u.content,u.reasoning_content!=null&&(a=u.reasoning_content),Array.isArray(u.tool_calls)&&(o=u.tool_calls),s=Si(u),l=u.stopping_word||u.stoppingWord||null,c=xi(u,o.length>0);else if(n===`error`)throw Error(u.message||`completion error`)}}finally{r.cancel().catch(()=>{})}let u=[];a&&u.push({type:`thinking`,thinking:a,signature:``}),i&&u.push({type:`text`,text:i});for(let e of o){let t={};try{t=JSON.parse(e.function?.arguments||`{}`)}catch{t={}}u.push({type:`tool_use`,id:e.id||`toolu_${Math.random().toString(36).slice(2,11)}`,name:e.function?.name||``,input:t})}return{id:t,type:`message`,role:`assistant`,content:u,model:n,stop_reason:c,stop_sequence:l,usage:s}}function wi({global:e}){let t=$r({prefix:`/anthropic-messages`});return t.use(M({origin:e?.anthropic_messages?.cors_allowed_origins??!1,methods:[`GET`,`POST`,`OPTIONS`],allowedHeaders:[`Content-Type`,`Authorization`,`x-api-key`,`anthropic-version`],maxAge:86400,preflight:!0})),t.post(`/v1/messages`,async function*({body:e,set:t,store:n}){let{config:r,backend:a}=n,o=e;try{if(!Array.isArray(o.messages)||o.messages.length===0)return t.status=400,{type:`error`,error:{type:`invalid_request_error`,message:`messages is required and must not be empty`}};let e=bi(o),n=await pi(a,r,o.model,`[Anthropic]`),s=yi(),c=n.repoId||`ggml-llm`,l={reasoning_format:`auto`,messages:e.messages,jinja:!0,add_generation_prompt:!0};e.temperature!=null&&(l.temperature=e.temperature),e.top_p!=null&&(l.top_p=e.top_p),e.top_k!=null&&(l.top_k=e.top_k),e.max_tokens!=null&&(l.n_predict=e.max_tokens),e.stop!=null&&(l.stop=e.stop),e.tools!=null&&(l.tools=e.tools),e.tool_choice!=null&&(l.tool_choice=e.tool_choice),l.enable_thinking=e.enable_thinking??!1,e.thinking_budget_tokens!=null&&(l.thinking_budget_tokens=e.thinking_budget_tokens);let u=await fi(a,n.type).completion(n.id,{options:l});if(!o.stream)return await Ci(u,s,c);let d=u.getReader(),f=``,p=``,m=new Map,h=new Map,g=new Map,_=new Set,v=!1,y=!1,b=0,x=0,S={cache_read_input_tokens:0,input_tokens:0,output_tokens:0},C=`end_turn`,w=null,T=!1,E=e=>(v?1:0)+(y?1:0)+e;try{let e=!1;for(;!e;){let t=await d.read();if({done:e}=t,e)break;let{event:n,data:r}=t.value;if(n===`token`){if(!T){let e=Si(r);yield i({event:`message_start`,data:JSON.stringify({type:`message_start`,message:{id:s,type:`message`,role:`assistant`,content:[],model:c,stop_reason:null,stop_sequence:null,usage:e}})}),T=!0}if(r.reasoning_content!=null){let e=r.reasoning_content;e.length>p.length&&(v||(yield i({event:`content_block_start`,data:JSON.stringify({type:`content_block_start`,index:0,content_block:{type:`thinking`,thinking:``}})}),v=!0,b=1),yield i({event:`content_block_delta`,data:JSON.stringify({type:`content_block_delta`,index:0,delta:{type:`thinking_delta`,thinking:e.slice(p.length)}})}),p=e)}if(r.content!=null){let e=r.content;e.length>f.length&&(y||=(yield i({event:`content_block_start`,data:JSON.stringify({type:`content_block_start`,index:b,content_block:{type:`text`,text:``}})}),!0),yield i({event:`content_block_delta`,data:JSON.stringify({type:`content_block_delta`,index:b,delta:{type:`text_delta`,text:e.slice(f.length)}})}),f=e)}if(Array.isArray(r.tool_calls)&&r.tool_calls.length>0){for(let e=0;e<r.tool_calls.length;e+=1){let t=r.tool_calls[e],n=E(e),a=t?.function?.arguments||``,o=m.get(e)||``;if(!_.has(e)){let r=t?.id||`toolu_${s}_${e}`,a=t?.function?.name||g.get(e)||``;h.set(e,r),g.set(e,a),_.add(e),yield i({event:`content_block_start`,data:JSON.stringify({type:`content_block_start`,index:n,content_block:{type:`tool_use`,id:r,name:a,input:{}}})})}a.length>o.length&&(yield i({event:`content_block_delta`,data:JSON.stringify({type:`content_block_delta`,index:n,delta:{type:`input_json_delta`,partial_json:a.slice(o.length)}})}),m.set(e,a))}x=r.tool_calls.length}}else if(n===`result`){if(!T){let e=Si(r);yield i({event:`message_start`,data:JSON.stringify({type:`message_start`,message:{id:s,type:`message`,role:`assistant`,content:[],model:c,stop_reason:null,stop_sequence:null,usage:e}})}),T=!0}Array.isArray(r.tool_calls)&&(x=Math.max(x,r.tool_calls.length)),S=Si(r),w=r.stopping_word||r.stoppingWord||null,C=xi(r,_.size>0)}else if(n===`error`){yield i({event:`error`,data:JSON.stringify({type:`error`,error:{type:`api_error`,message:r.message||`completion error`}})});return}}v&&(yield i({event:`content_block_delta`,data:JSON.stringify({type:`content_block_delta`,index:0,delta:{type:`signature_delta`,signature:``}})}),yield i({event:`content_block_stop`,data:JSON.stringify({type:`content_block_stop`,index:0})})),y&&(yield i({event:`content_block_stop`,data:JSON.stringify({type:`content_block_stop`,index:b})}));for(let e of[..._].sort((e,t)=>e-t))yield i({event:`content_block_stop`,data:JSON.stringify({type:`content_block_stop`,index:E(e)})});yield i({event:`message_delta`,data:JSON.stringify({type:`message_delta`,delta:{stop_reason:C,stop_sequence:w},usage:{output_tokens:S.output_tokens}})}),yield i({event:`message_stop`,data:JSON.stringify({type:`message_stop`})})}finally{d.cancel().catch(()=>{})}}catch(e){return console.error(`[Anthropic] Messages error:`,e),t.status=500,{type:`error`,error:{type:`api_error`,message:e?.message||`Internal server error`}}}},{body:a.Object({model:a.Optional(a.String()),messages:a.Array(a.Any()),system:a.Optional(a.Any()),max_tokens:a.Optional(a.Number()),stream:a.Optional(a.Boolean()),temperature:a.Optional(a.Number()),top_p:a.Optional(a.Number()),top_k:a.Optional(a.Number()),stop_sequences:a.Optional(a.Union([a.String(),a.Array(a.String())])),tools:a.Optional(a.Array(a.Any())),tool_choice:a.Optional(a.Any()),thinking:a.Optional(a.Any()),metadata:a.Optional(a.Any())})}),t.post(`/v1/messages/count_tokens`,async({body:e,set:t,store:n})=>{let{config:r,backend:i}=n,a=e;try{let e=bi(a),t=await pi(i,r,a.model,`[Anthropic]`),n=fi(i,t.type),o={messages:e.messages,add_generation_prompt:!0,jinja:!0};e.tools!=null&&(o.tools=e.tools);let s=await n.applyChatTemplate(t.id,o),c=typeof s==`string`?s:s?.prompt||``,l=await n.tokenize(t.id,{text:c,add_special:!0,parse_special:!0});return{input_tokens:(Array.isArray(l)?l:l?.tokens||[]).length}}catch(e){return console.error(`[Anthropic] count_tokens error:`,e),t.status=500,{type:`error`,error:{type:`api_error`,message:e?.message||`Internal server error`}}}},{body:a.Object({model:a.Optional(a.String()),messages:a.Array(a.Any()),system:a.Optional(a.Any()),tools:a.Optional(a.Array(a.Any())),tool_choice:a.Optional(a.Any())})}),t.get(`/v1/models`,({store:e})=>{let{config:t}=e,n=(t.generators||[]).filter(e=>ui.includes(e.type)).map(e=>{let t=e.model?.repo_id||e.type;return{id:t,type:`model`,display_name:t,created_at:new Date().toISOString()}});return n.length===0&&n.push({id:`ggml-llm`,type:`model`,display_name:`ggml-llm`,created_at:new Date().toISOString()}),{data:n,has_more:!1,first_id:n[0]?.id,last_id:n.at(-1)?.id}}),t}const Ti=(e={},t={})=>{let n=Array.isArray(e)?[...e]:{...e};return Object.entries(t||{}).forEach(([e,t])=>{t&&typeof t==`object`&&!Array.isArray(t)?n[e]=Ti(n[e]||{},t):n[e]=t}),n},Ei=e=>e&&typeof e==`object`?structuredClone(e):null,Di=(e,t)=>Ti(Ei(e)||{},Ei(t)||{}),Oi=(e,t)=>Ti(structuredClone(e.global),t||{}),ki=(e,t,n,r)=>{if(e.generators.length>0){let i=e.generators.filter(e=>e?.type===n);if(i.length>0&&r){let a=i.find(e=>t.getModelIdentifier(n,e)===r);if(a)return Oi(e,a)}}return Object.keys(e.global).length>0?Oi(e,{}):null},Ai={udp:{port:8089,announcements:{enabled:!0,interval:5e3},requests:{enabled:!0,responseDelay:100}},http:{enabled:!0,path:`/buttress/info`,cors:!0}},ji=e=>e?e===!0?{...Ai}:Ti(Ai,e):null,Mi=(e,t)=>{if(!e.generators||e.generators.length===0)return t.map(e=>({type:e}));let n=new Set;return e.generators.forEach(e=>{e.type&&n.add(e.type)}),n.size===0?t.map(e=>({type:e})):Array.from(n).map(e=>({type:e}))},Ni=(e,t,n)=>e===void 0?n:typeof e==`number`?e:t(e)??n,Pi=6e4,Fi=1024*1024*50,Ii=e=>{let t=N.machineIdSync(),n=Ti({server:{id:`buttress-${t}`,name:`Buttress Server (${t.slice(-8)})`,port:2080,temp_file_dir:_.join(v.tmpdir(),`.buttress`),session_timeout:Pi,max_body_size:Fi},autodiscover:!1},Ei(e)||{}),r=Array.isArray(n.generators)?n.generators:[],{server:i,generators:a,autodiscover:o,...s}=n;return{autodiscover:ji(o),server:{id:i.id,name:i.name,port:i.port,log_level:i.log_level,temp_file_dir:i.temp_file_dir,max_body_size:Ni(i.max_body_size,w.parse,Fi),session_timeout:Ni(i.session_timeout,P,Pi)},global:s,generators:r}},Li={getCapabilities:j.tuple([j.object({type:j.string().optional().default(`ggml-llm`),config:j.any().optional(),currentClientCapabilities:j.any().optional(),options:j.any().optional()}).nullable().optional()]),startGenerator:j.tuple([j.string(),j.any().optional()]),finalizeGenerator:j.tuple([j.string()])};var Ri={async getCapabilities({backend:e,config:t},n=null){console.log(`[Server] Get Capabilities:`,n);let{type:r=`ggml-llm`,config:i,currentClientCapabilities:a=null,options:o={}}=n||{type:`ggml-llm`},s=Ei(i),c=Di(ki(t,e,r,e.getModelIdentifier(r,s)),i);if(Object.keys(c).length===0)throw Error(`Buttress server missing generator configuration`);return c.backend=c.backend||{},c.backend.type||(c.backend.type=r),e.getCapabilities(r,a,{...o,config:c})},async startGenerator({backend:e,config:t,session:n},r,i){console.log(`[Server] Start Generator:`,r,i);let a=Ei(i),o=Di(ki(t,e,r,e.getModelIdentifier(r,a)),i);if(Object.keys(o).length===0)throw Error(`Buttress server missing generator configuration`);o.backend=o.backend||{},o.backend.type||(o.backend.type=r);let s=await e.startGenerator(r,o);return n.generators.add(s.id),s},async finalizeGenerator({backend:e,session:t},n){return console.log(`[Server] Finalize Generator:`,n),t.generators.delete(n),e.finalizeGenerator(n)}};const zi={initContext:j.tuple([j.string(),j.any().optional()]),completion:j.tuple([j.string(),j.any().optional()]),tokenize:j.tuple([j.string(),j.any()]),detokenize:j.tuple([j.string(),j.any()]),applyChatTemplate:j.tuple([j.string(),j.any()]),releaseContext:j.tuple([j.string()])};function Bi(e){return function({backend:t,session:n},r,i){return new s({async start(a){try{let o=await e(t).initContext(r,{...i,onProgress:e=>{a.enqueue({event:`progress`,data:{progress:e}})}});n.initializedContexts.add(r),await new Promise(e=>setTimeout(e,1e3));let{download:s,...c}=o||{};a.enqueue({event:`result`,data:{result:c}}),a.close()}catch(e){a.error(e)}}})}}function Vi(e,t){return async function({backend:n,session:r},i,a){return console.log(`[Server] ${t}:`,{id:i,force:a}),r.initializedContexts.has(i)?(r.initializedContexts.delete(i),e(n).releaseContext(i,{force:a})):(console.log(`[Server] ${t} skipped - not initialized by this session:`,{id:i}),{released:!1,skipped:!0})}}function Hi(e,t){return{initContext:Bi(e),completion({backend:n},r,i){return console.log(`[Server] ${t}Completion:`,{id:r,property:i}),e(n).completion(r,i)},async tokenize({backend:n},r,i){return console.log(`[Server] ${t}Tokenize:`,{id:r,property:i}),e(n).tokenize(r,i)},async detokenize({backend:n},r,i){return console.log(`[Server] ${t}Detokenize:`,{id:r,property:i}),e(n).detokenize(r,i)},async applyChatTemplate({backend:n},r,i){return console.log(`[Server] ${t}Apply Chat Template:`,{id:r,property:i}),e(n).applyChatTemplate(r,i)},releaseContext:Vi(e,`${t}Release Context`)}}var Ui=Hi(e=>e.ggmlLlm,``);const Wi={initContext:j.tuple([j.string(),j.any().optional()]),transcribe:j.tuple([j.string(),j.string(),j.any().optional()]),transcribeData:j.tuple([j.string(),j.union([j.instanceof(Buffer),j.instanceof(Uint8Array)]),j.any().optional()]),releaseContext:j.tuple([j.string()])},Gi=e=>e.ggmlStt,Ki={common:Ri,ggmlLlm:Ui,ggmlStt:{initContext:Bi(Gi),async transcribe({backend:e,config:{server:t}},n,r,i){return console.log(`[Server] Transcribe:`,{id:n,audioPath:r,options:i}),e.ggmlStt.transcribe(n,{audioPath:_.join(t.temp_file_dir,r),options:i})},async transcribeData({backend:e},t,n,r){return console.log(`[Server] Transcribe Data:`,{id:t,audioDataLength:n?.length||0,options:r}),e.ggmlStt.transcribeData(t,{audioData:n,options:r})},releaseContext:Vi(Gi,`Release STT Context`)},mlxLlm:Hi(e=>e.mlxLlm,`MLX `)},qi={common:Li,ggmlLlm:zi,ggmlStt:Wi,mlxLlm:zi};var Ji=Ki;const Yi=e=>{try{return JSON.parse(e,(e,t)=>{if(!t)return t;if(t?.type===`Buffer`&&t?.data)return F.from(t.data,`base64`);if(t?.type===`Uint8Array`&&t?.data){let e=F.from(t.data,`base64`);return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}return t?.type===`Error`&&t?.name&&t?.message?Error(t.name,t.message):t})}catch{return e}},Xi=e=>{try{return JSON.stringify(e,(e,t)=>t instanceof Error?{type:`Error`,name:t.name,message:t.message}:t instanceof F?{type:`Buffer`,data:t.toString(`base64`)}:t instanceof Uint8Array?{type:`Uint8Array`,data:F.from(t).toString(`base64`)}:t)}catch{return e}};var Zi=class{name=`udp`;socket=null;announcementTimer=null;config;getServerInfo;port;constructor(e,t){this.config=e,this.getServerInfo=t,this.port=e.port??8089}async start(){if(this.socket=re.createSocket({type:`udp4`,reuseAddr:!0}),this.socket.on(`message`,(e,t)=>{this.handleMessage(e,t)}),this.socket.on(`error`,e=>{console.error(`[Autodiscover UDP] Socket error:`,e.message)}),await new Promise((e,t)=>{this.socket.bind(this.port,()=>{this.socket.setBroadcast(!0),console.log(`[Autodiscover UDP] Listening on port ${this.port}`),e()}),this.socket.once(`error`,t)}),this.config.announcements.enabled){let e=this.config.announcements.interval??5e3;this.announcementTimer=setInterval(()=>{this.sendAnnouncement()},e),this.sendAnnouncement()}}async stop(){this.announcementTimer&&=(clearInterval(this.announcementTimer),null),this.socket&&=(await new Promise(e=>{this.socket.close(()=>e())}),null)}handleMessage(e,t){try{let n=JSON.parse(e.toString());if(n.t===`QUERY`&&this.config.requests.enabled){let e=n.d,r=this.config.requests.responseDelay??0,i=r>0?Math.random()*r:0;setTimeout(()=>{this.sendResponse(e.id,t)},i)}}catch{}}sendAnnouncement(){if(!this.socket)return;let e={t:`ANNOUNCE`,v:`1.0`,d:{info:this.getServerInfo()}},t=Buffer.from(JSON.stringify(e));this.socket.send(t,0,t.length,this.port,`255.255.255.255`,e=>{e&&console.error(`[Autodiscover UDP] Announcement error:`,e.message)})}sendResponse(e,t){if(!this.socket)return;let n={t:`RESPONSE`,v:`1.0`,d:{request_id:e,info:this.getServerInfo()}},r=Buffer.from(JSON.stringify(n));this.socket.send(r,0,r.length,t.port,t.address,e=>{e&&console.error(`[Autodiscover UDP] Response error:`,e.message)})}},Qi=class{transports=[];started=!1;constructor(e,t){this.config=e,this.getServerInfo=t,(e.udp?.announcements?.enabled||e.udp?.requests?.enabled)&&this.transports.push(new Zi(e.udp,t))}async start(){this.started||=((await Promise.allSettled(this.transports.map(e=>e.start()))).forEach((e,t)=>{e.status===`rejected`&&console.error(`[Autodiscover] Failed to start ${this.transports[t].name}:`,e.reason)}),!0)}async stop(){this.started&&=(await Promise.allSettled(this.transports.map(e=>e.stop())),!1)}};const $i=()=>{let e=v.networkInterfaces();return Object.values(e).flat().find(e=>e?.family===`IPv4`&&!e?.internal)?.address||null},$=Zr(),ea=e=>{if(!e)return{repoId:null,filename:null};let[t,n]=e.split(`:`);return{repoId:t,filename:n||null}};async function ta({modelIds:e=[],defaultConfig:t=null}={}){let n=[];console.log(`${$.name} v${$.version}`),console.log(`Generating model capabilities comparison...
|
|
37
37
|
`),n.push(`${$.name} v${$.version}`),n.push(`## Model Capabilities Comparison
|
|
38
38
|
`),(!e||e.length===0)&&(console.error(`Error: No model IDs provided`),process.exit(1));try{let r=(e={},t={})=>{let n=Array.isArray(e)?[...e]:{...e};return Object.entries(t||{}).forEach(([e,t])=>{t&&typeof t==`object`&&!Array.isArray(t)?n[e]=r(n[e]||{},t):n[e]=t}),n},{server:i,generators:a=[],...o}=t||{},s=e=>r(structuredClone(o),e||{}),c=e=>{if(Array.isArray(a)&&a.length>0){let t=a.filter(e=>e?.type===`ggml-llm`);if(t.length>0&&e){let n=t.find(t=>t.model?.repo_id===e);if(n)return s(n)}}return Object.keys(o).length>0?s({}):null},l=[];for(let t=0;t<e.length;t+=1){let n=e[t];console.log(`[${t+1}/${e.length}] Analyzing ${n}...`);let r=c(n);r={...r||{},model:{...o.runtime,...r?.model||{},repo_id:n}};let i=await J(`ggml-llm`,null,{config:r,includeBreakdown:!0});l.push({modelId:n,capabilities:i,modelInfo:i.buttress?.selected||null,modelConfig:i.modelConfig||null})}let u=e=>e?(e/1024/1024/1024).toFixed(2):`N/A`,d=e=>e?`✅`:`🚫`;n.push(`| Model ID | Size (GB) | Context Size | KV Cache Size (GB) | Recurrent Mem (GB) | Total Required Memory (GB) | Fits GPU (Full) | Fits CPU (Full) |`),n.push(`|----------|-----------|--------------|--------------------|--------------------|----------------------------|-----------------|-----------------|`),l.forEach(({modelId:e,modelInfo:t,modelConfig:r})=>{let i=u(t?.modelBytes),a=r?.nCtx||t?.kvInfo?.nCtxTrain||`N/A`,o=Ue(t),s=Number(a),c=t?.kvCacheBytes||(o&&Number.isFinite(s)&&s>0?o(s):o&&o(t?.kvInfo?.nCtxTrain||0))||null,l=u(c),f=t?.recurrentMemoryBytes||0,p=f>0?u(f):`-`,m=u(t?.modelBytes&&(c!=null||f>0)?t.modelBytes+(c||0)+f:t?.fit?.totalRequiredBytes),h=d(t?.fit?.fitsInGpu),g=d(t?.fit?.fitsInCpu);n.push(`| ${e} | ${i} | ${a} | ${l} | ${p} | ${m} | ${h} | ${g} |`);let _=t?.memoryLimitedCtx!=null||t?.limitedFit!=null,v=!t?.fit?.fitsInGpu||!t?.fit?.fitsInCpu;if(_&&v){let e=t?.memoryLimitedCtx||a,r=Number(e),s=t?.limitedKvCacheBytes||o&&Number.isFinite(r)&&r>0&&o(r)||null,c=u(s),h=u(t?.modelBytes&&(s!=null||f>0)?t.modelBytes+(s||0)+f:t?.limitedFit?.totalRequiredBytes),g=d(t?.limitedFit?.fitsInGpu),_=d(t?.limitedFit?.fitsInCpu);(e!==a||c!==l||h!==m)&&n.push(`| ↳ Limited | ${i} | ${e} | ${c} | ${p} | ${h} | ${g} | ${_} |`)}}),n.push(`
|
|
39
39
|
---`),n.push(`
|
|
@@ -99,9 +99,7 @@ Testing Options:
|
|
|
99
99
|
file (e.g., ggml-llm-model-capabilities-YYYY-MM-DD.md)
|
|
100
100
|
|
|
101
101
|
Environment Variables:
|
|
102
|
-
NODE_ENV
|
|
103
|
-
ENABLE_OPENAI_COMPAT_ENDPOINT Set to '1' to enable OpenAI-compatible API
|
|
104
|
-
ENABLE_ANTHROPIC_MESSAGES_ENDPOINT Set to '1' to enable Anthropic Messages API
|
|
102
|
+
NODE_ENV Set to 'development' for dev mode
|
|
105
103
|
|
|
106
104
|
Examples:
|
|
107
105
|
bricks-buttress
|
|
@@ -111,4 +109,4 @@ Examples:
|
|
|
111
109
|
bricks-buttress --test-caps ggml-stt --test-caps-model-id BricksDisplay/whisper-ggml:ggml-small.bin
|
|
112
110
|
bricks-buttress --test-caps mlx-llm --test-models-default
|
|
113
111
|
`),process.exit(0));let e=process.argv.findIndex(e=>e===`--port`||e===`-p`),t=e>=0?Number(process.argv[e+1]):void 0,n=process.argv.findIndex(e=>e===`--config`||e===`-c`),r=n>=0?process.argv[n+1]:null,i=null;if(r){let e;if(r.includes(`
|
|
114
|
-
`))e=r;else{let t=_.resolve(r);try{e=k.readFileSync(t,`utf8`)}catch(e){console.error(`Failed to read Buttress config at ${t}:`,e),process.exit(1)}}try{let t=A.parse(e);t.env&&typeof t.env==`object`&&(Object.entries(t.env).forEach(([e,t])=>{process.env[e]===void 0&&(process.env[e]=String(t))}),delete t.env),i=t}catch(e){console.error(`Failed to parse TOML config:`,e),process.exit(1)}}async function a(e){if(!e?.generators||!Array.isArray(e.generators))return;let t=e.generators.filter(e=>{if(!e.model?.download)return!1;let{type:t}=e;return!t||t!==`ggml-llm`&&t!==`ggml-stt`&&t!==`mlx-llm`?(console.warn(`[Download] Skipping unknown generator type: ${t}`),!1):!0});if(t.length===0)return;let{server:n,generators:r,...i}=e,a=t.map(e=>{let{type:t}=e,n=e.model?.repo_id;return console.log(`[Download] Starting pre-download for ${t}: ${n}`),qr(t,{...i,backend:e.backend||{},model:e.model||{},runtime:{...i.runtime,...e.runtime||{}}},{onProgress:()=>{},onComplete:({repoId:e,alreadyExists:t})=>{t?console.log(`[Download] Pre-download complete (already exists): ${e}`):console.log(`[Download] Pre-download complete: ${e}`)},onError:e=>{console.error(`[Download] Pre-download failed for ${n}:`,e.message)}})}),o=await Promise.all(a),s=o.filter(e=>e.started).length,c=o.filter(e=>e.alreadyExists).length,l=o.filter(e=>e.alreadyDownloading).length;console.log(`[Download] Pre-download summary: ${s} started, ${c} already exist, ${l} already downloading`)}let o=[`ggml-org/gpt-oss-20b-GGUF`,`ggml-org/gpt-oss-120b-GGUF`,`unsloth/Nemotron-3-Nano-30B-A3B-GGUF`,`unsloth/Qwen3-30B-A3B-Instruct-2507-GGUF`,`unsloth/Qwen3.5-27B-GGUF`,`unsloth/gemma-4-26B-A4B-it-GGUF`,`unsloth/gemma-4-31B-it-GGUF`,`unsloth/GLM-4.7-Flash-GGUF`,`DevQuasar/MiniMaxAI.MiniMax-M2.5-GGUF`,`bartowski/Mistral-Nemo-Instruct-2407-GGUF`,`mistralai/Magistral-Small-2509-GGUF`,`mistralai/Ministral-3-14B-Reasoning-2512-GGUF`,`bartowski/mistralai_Devstral-Small-2-24B-Instruct-2512-GGUF`,`bartowski/mistralai_Devstral-2-123B-Instruct-2512-GGUF`,`ggml-org/gemma-3-12b-it-qat-GGUF`,`ggml-org/gemma-3-27b-it-qat-GGUF`,`unsloth/phi-4-GGUF`],s=[`BricksDisplay/whisper-ggml:ggml-small.bin`,`BricksDisplay/whisper-ggml:ggml-small-q8_0.bin`,`BricksDisplay/whisper-ggml:ggml-medium.bin`,`BricksDisplay/whisper-ggml:ggml-medium-q8_0.bin`,`BricksDisplay/whisper-ggml:ggml-large-v3-turbo.bin`,`BricksDisplay/whisper-ggml:ggml-large-v3-turbo-q8_0.bin`,`BricksDisplay/whisper-ggml:ggml-large-v3.bin`],c=[`mlx-community/Qwen3.5-27B-8bit`,`mlx-community/Qwen3.5-27B-4bit`,`mlx-community/Qwen3.5-35B-A3B-8bit`,`mlx-community/Qwen3.5-35B-A3B-4bit`,`mlx-community/Qwen3-235B-A22B-8bit`,`mlx-community/Qwen3-235B-A22B-4bit`,`mlx-community/GLM-4.7-Flash-8bit`,`mlx-community/GLM-4.7-Flash-4bit`,`mlx-community/MiniMax-M2.5-4bit`,`mlx-community/gpt-oss-120b-4bit`,`mlx-community/gemma-4-26b-a4b-it-8bit`,`mlx-community/gemma-4-26b-a4b-it-4bit`,`mlx-community/gemma-4-31b-it-8bit`,`mlx-community/gemma-4-31b-it-4bit`],l=process.argv.findIndex(e=>e===`--test-caps`);if(l>=0){let e=process.argv[l+1]||`ggml-llm`;e!==`ggml-llm`&&e!==`ggml-stt`&&e!==`mlx-llm`&&(console.error(`Only ggml-llm, ggml-stt, and mlx-llm backends are supported for testing capabilities`),process.exit(1));let t=process.argv.findIndex(e=>e===`--test-models`),n=process.argv.includes(`--test-models-default`);if(e===`mlx-llm`)if(t>=0){let e=process.argv[t+1];e||(console.error(`Error: --test-models requires a comma-separated list of model IDs`),process.exit(1)),await ia({modelIds:e.split(`,`).map(e=>e.trim()),defaultConfig:i})}else if(n)await ia({modelIds:c,defaultConfig:i});else{let e=process.argv.findIndex(e=>e===`--test-caps-model-id`);await aa({modelId:e>=0?process.argv[e+1]:null,defaultConfig:i})}else if(e===`ggml-stt`)if(t>=0){let e=process.argv[t+1];e||(console.error(`Error: --test-models requires a comma-separated list of model IDs`),process.exit(1)),await ra({modelIds:e.split(`,`).map(e=>e.trim()),defaultConfig:i})}else if(n)await ra({modelIds:s,defaultConfig:i});else{let e=process.argv.findIndex(e=>e===`--test-caps-model-id`);await oa({modelId:e>=0?process.argv[e+1]:null,defaultConfig:i})}else if(t>=0){let e=process.argv[t+1];e||(console.error(`Error: --test-models requires a comma-separated list of model IDs`),process.exit(1)),await ta({modelIds:e.split(`,`).map(e=>e.trim()),defaultConfig:i})}else if(n)await ta({modelIds:o,defaultConfig:i});else{let e=process.argv.findIndex(e=>e===`--test-caps-model-id`);await na({modelId:e>=0?process.argv[e+1]:null,defaultConfig:i})}}let u=Ii(i);t&&(u.server.port=t),u.server.port||(u.server.port=2080),_a({config:u,enableOpenAICompat:process.env.ENABLE_OPENAI_COMPAT_ENDPOINT===`1`,enableAnthropicMessages:process.env.ENABLE_ANTHROPIC_MESSAGES_ENDPOINT===`1`}).then(async({port:e,openaiEnabled:t,anthropicMessagesEnabled:n,autoDiscover:r})=>{let o=$i();console.log(`Buttress server listening on port ${e}`),console.log(`--------------------------------`),await ma(),console.log();let s={"ggml-llm":`LLM (GGML)`,"ggml-stt":`STT (GGML)`,"mlx-llm":`LLM (MLX)`};console.log(`Current supported Generators:`);let c=new Set((u?.generators||[]).map(e=>e.type).filter(Boolean));if(c.size===0)console.log(`- LLM (GGML)`),console.log(`- STT (GGML)`);else for(let e of c)console.log(`- ${s[e]||e}`);console.log(),console.log("Please configure `Buttress (Remote Inference)` in the Generator to connect to this server."),console.log(),console.log(`- Use http://${o}:${e} to connect to this server via LAN.`),console.log(`- Visit http://${o}:${e}/status to see status via LAN.`),console.log(),t?(console.log(`OpenAI-compatible API [EXPERIMENTAL]:`),console.log(`- Base URL: http://${o}:${e}/oai-compat/v1`),console.log(`- Chat completions: POST http://${o}:${e}/oai-compat/v1/chat/completions`),console.log(`- Models: GET http://${o}:${e}/oai-compat/v1/models`),console.log()):(console.log(`OpenAI-compatible API [EXPERIMENTAL]: disabled`),console.log(` Set ENABLE_OPENAI_COMPAT_ENDPOINT=1 to enable`),console.log()),n?(console.log(`Anthropic Messages API [EXPERIMENTAL]:`),console.log(`- Base URL: http://${o}:${e}/anthropic-messages`),console.log(`- Messages: POST http://${o}:${e}/anthropic-messages/v1/messages`),console.log(`- Count tokens: POST http://${o}:${e}/anthropic-messages/v1/messages/count_tokens`),console.log()):(console.log(`Anthropic Messages API [EXPERIMENTAL]: disabled`),console.log(` Set ENABLE_ANTHROPIC_MESSAGES_ENDPOINT=1 to enable`),console.log()),r&&(console.log(`Auto-discover enabled`),console.log()),i&&await a(i)}).catch(e=>{console.error(`Failed to start Buttress server:`,e),process.exitCode=1})};const{version:la,name:ua}=Zr(),da=async()=>{let e=`https://registry.npmjs.org/${ua}/latest`;try{let t=new AbortController,n=setTimeout(()=>t.abort(),3e3),r=await fetch(e,{headers:{Accept:`application/json`},signal:t.signal});return clearTimeout(n),r.ok&&(await r.json()).version||null}catch{return null}},fa=(e,t)=>{if(!t)return!1;let n=e.split(/[.-]/),r=t.split(/[.-]/);for(let e=0;e<Math.max(n.length,r.length);e+=1){let t=parseInt(n[e])||0,i=parseInt(r[e])||0;if(i>t)return!0;if(i<t)return!1}return!1},pa=e=>{console.log(``),console.log(`\x1B[33mâ•─────────────────────────────────────────────────╮\x1B[0m`),console.log(`\x1B[33m│\x1B[0m Update available! \x1B[2m%s\x1B[0m → \x1B[32m%s\x1B[0m`,la.padEnd(12),e.padEnd(12),`\x1B[33m│\x1B[0m`),console.log(`\x1B[33m│\x1B[0m \x1B[33m│\x1B[0m`),console.log(`\x1B[33m│\x1B[0m Run to upgrade: \x1B[33m│\x1B[0m`),console.log(`\x1B[33m│\x1B[0m \x1B[36mnpm install -g %s\x1B[0m \x1B[33m│\x1B[0m`,ua.padEnd(27)),console.log(`\x1B[33m╰─────────────────────────────────────────────────╯\x1B[0m`),console.log(``)},ma=async()=>{try{let e=await da();e&&fa(la,e)&&pa(e)}catch{}},ha=typeof process<`u`&&process.versions&&process.versions.node,ga=async({backend:e,router:r,config:i,enableOpenAICompat:o,enableAnthropicMessages:l})=>{try{await c.mkdir(i.server.temp_file_dir,{recursive:!0})}catch{}let u=$i()||`0.0.0.0`,d={id:i.server.id,name:i.server.name,version:la,address:u,port:i.server.port,url:`http://${u}:${i.server.port}`,generators:Mi(i,i.generators.map(e=>e.type)),authentication:{required:!0,type:`device-group`}},f=new n({serve:{maxRequestBodySize:i.server.max_body_size},websocket:{idleTimeout:Math.ceil(i.server.session_timeout/1e3)},adapter:ha?t():void 0}).state({sessions:new Map,backend:e||Lr,config:i,serverInfo:d});r&&f.use(r),i.autodiscover?.http?.enabled&&f.use(ni(i)),f.use(ii),f.use(li),o&&f.use(vi(i)),l&&f.use(wi(i));let p={INVALID_REQUEST:-32600,INVALID_PARAMS:-32602,METHOD_NOT_FOUND:-32601,INTERNAL_ERROR:-32603},m=e=>e.id??e.raw?.id??e.remoteAddress;return f.ws(`/buttress/rpc`,{parse:(e,t)=>{if(typeof t==`string`)try{return JSON.parse(t)}catch{return e.send(JSON.stringify({jsonrpc:`2.0`,error:{code:p.INVALID_REQUEST,message:`Invalid request`},id:null})),null}return t},body:a.Object({jsonrpc:a.String(),method:a.String(),params:a.String(),id:a.String()}),open(e){let t=m(e);if(console.log(`[Request] New connection: ${t}`),!e.data.store.sessions.has(t))e.data.store.sessions.set(t,{streamReaders:new Map,generators:new Set,initializedContexts:new Set,timeout:null});else{let n=e.data.store.sessions.get(t);n&&(clearTimeout(n.timeout),n.timeout=null)}},async message(e,{id:t,method:n,params:r}){let i=m(e);console.log(`[Request] Received request from ${i}: ${n}`);let a=e.data.store.sessions.get(i),[o,c]=n.split(`.`),l=Ji[o]?.[c];if(!l){e.send(JSON.stringify({jsonrpc:`2.0`,error:{code:p.METHOD_NOT_FOUND,message:`Method not found`},id:t}));return}try{if(n===`cancel`){a.streamReaders.has(t)&&(a.streamReaders.get(t)?.cancel(),a.streamReaders.delete(t));return}if(n===`ping`){e.send(JSON.stringify({jsonrpc:`2.0`,result:`pong`,id:t}));return}let u=Yi(r),d=qi[o]?.[c],f=d?d.parse(u):u,p=await l({...e.data.store,peerId:i,session:a},...f);if(p instanceof s){let r=p.getReader();a.streamReaders.set(t,r),e.send(JSON.stringify({jsonrpc:`2.0`,result:{type:`stream`},id:t}));try{for(;;){let{value:n,done:i}=await r.read();if(i)break;let{event:a,data:o}=n;e.send(JSON.stringify({jsonrpc:`2.0`,method:`notification/${a}`,params:Xi(o),id:t}))}e.send(JSON.stringify({jsonrpc:`2.0`,method:`notification/_end`,id:t}))}catch(r){console.error(`[RPC] Stream error for ${n}:`,r),e.send(JSON.stringify({jsonrpc:`2.0`,method:`notification/_error`,params:Xi(r),id:t}))}a.streamReaders.delete(t)}else e.send(JSON.stringify({jsonrpc:`2.0`,result:Xi(p),id:t}))}catch(r){if(r instanceof ne){e.send(JSON.stringify({jsonrpc:`2.0`,error:{code:p.INVALID_PARAMS,message:`Invalid params`,data:r.issues},id:t}));return}console.error(`[RPC] Handler error for ${n}:`,r),e.send(JSON.stringify({jsonrpc:`2.0`,error:{code:p.INTERNAL_ERROR,message:String(r)},id:t}))}},async close(e){let t=m(e);console.log(`[Request] Connection closed: ${t}`);let{backend:n,sessions:r}=e.data.store,a=r.get(t);a&&(a.streamReaders.forEach(e=>e.cancel()),a.streamReaders.clear(),a.timeout=setTimeout(()=>{r.delete(t),console.log(`[Request] Session timed out: ${t}`),a.generators.forEach(e=>{n.finalizeGenerator(e)})},i.server.session_timeout))}}),{app:f,config:i}},_a=async({backend:e,router:t,config:n,enableOpenAICompat:r=!1,enableAnthropicMessages:i=!1})=>{let{app:a,config:o}=await ga({backend:e,router:t,config:n,enableOpenAICompat:r,enableAnthropicMessages:i}),{server:{port:s}}=o,c=[new Promise(e=>a.listen(s,e))],l=null;return o.autodiscover&&(l=new Qi(o.autodiscover,()=>a.store.serverInfo),c.push(l.start())),await Promise.all(c),{app:a,port:s,openaiEnabled:r,anthropicMessagesEnabled:i,autoDiscover:l}},va=[new URL(`index.mjs`,import.meta.url).pathname,new URL(`index.ts`,import.meta.url).pathname];(process.argv[1]?.endsWith(`/bricks-buttress`)||va.includes(process.argv[1]))&&await ca();export{ma as checkAndNotifyUpdates,da as checkForUpdates,fa as compareVersions,ga as createServer,pa as logUpdateMessage,Ii as processConfig,qr as startModelDownload,_a as startServer};
|
|
112
|
+
`))e=r;else{let t=_.resolve(r);try{e=k.readFileSync(t,`utf8`)}catch(e){console.error(`Failed to read Buttress config at ${t}:`,e),process.exit(1)}}try{let t=A.parse(e);t.env&&typeof t.env==`object`&&(Object.entries(t.env).forEach(([e,t])=>{process.env[e]===void 0&&(process.env[e]=String(t))}),delete t.env),i=t}catch(e){console.error(`Failed to parse TOML config:`,e),process.exit(1)}}async function a(e){if(!e?.generators||!Array.isArray(e.generators))return;let t=e.generators.filter(e=>{if(!e.model?.download)return!1;let{type:t}=e;return!t||t!==`ggml-llm`&&t!==`ggml-stt`&&t!==`mlx-llm`?(console.warn(`[Download] Skipping unknown generator type: ${t}`),!1):!0});if(t.length===0)return;let{server:n,generators:r,...i}=e,a=t.map(e=>{let{type:t}=e,n=e.model?.repo_id;return console.log(`[Download] Starting pre-download for ${t}: ${n}`),qr(t,{...i,backend:e.backend||{},model:e.model||{},runtime:{...i.runtime,...e.runtime||{}}},{onProgress:()=>{},onComplete:({repoId:e,alreadyExists:t})=>{t?console.log(`[Download] Pre-download complete (already exists): ${e}`):console.log(`[Download] Pre-download complete: ${e}`)},onError:e=>{console.error(`[Download] Pre-download failed for ${n}:`,e.message)}})}),o=await Promise.all(a),s=o.filter(e=>e.started).length,c=o.filter(e=>e.alreadyExists).length,l=o.filter(e=>e.alreadyDownloading).length;console.log(`[Download] Pre-download summary: ${s} started, ${c} already exist, ${l} already downloading`)}let o=[`ggml-org/gpt-oss-20b-GGUF`,`ggml-org/gpt-oss-120b-GGUF`,`unsloth/Nemotron-3-Nano-30B-A3B-GGUF`,`unsloth/Qwen3-30B-A3B-Instruct-2507-GGUF`,`unsloth/Qwen3.5-27B-GGUF`,`unsloth/gemma-4-26B-A4B-it-GGUF`,`unsloth/gemma-4-31B-it-GGUF`,`unsloth/GLM-4.7-Flash-GGUF`,`DevQuasar/MiniMaxAI.MiniMax-M2.5-GGUF`,`bartowski/Mistral-Nemo-Instruct-2407-GGUF`,`mistralai/Magistral-Small-2509-GGUF`,`mistralai/Ministral-3-14B-Reasoning-2512-GGUF`,`bartowski/mistralai_Devstral-Small-2-24B-Instruct-2512-GGUF`,`bartowski/mistralai_Devstral-2-123B-Instruct-2512-GGUF`,`ggml-org/gemma-3-12b-it-qat-GGUF`,`ggml-org/gemma-3-27b-it-qat-GGUF`,`unsloth/phi-4-GGUF`],s=[`BricksDisplay/whisper-ggml:ggml-small.bin`,`BricksDisplay/whisper-ggml:ggml-small-q8_0.bin`,`BricksDisplay/whisper-ggml:ggml-medium.bin`,`BricksDisplay/whisper-ggml:ggml-medium-q8_0.bin`,`BricksDisplay/whisper-ggml:ggml-large-v3-turbo.bin`,`BricksDisplay/whisper-ggml:ggml-large-v3-turbo-q8_0.bin`,`BricksDisplay/whisper-ggml:ggml-large-v3.bin`],c=[`mlx-community/Qwen3.5-27B-8bit`,`mlx-community/Qwen3.5-27B-4bit`,`mlx-community/Qwen3.5-35B-A3B-8bit`,`mlx-community/Qwen3.5-35B-A3B-4bit`,`mlx-community/Qwen3-235B-A22B-8bit`,`mlx-community/Qwen3-235B-A22B-4bit`,`mlx-community/GLM-4.7-Flash-8bit`,`mlx-community/GLM-4.7-Flash-4bit`,`mlx-community/MiniMax-M2.5-4bit`,`mlx-community/gpt-oss-120b-4bit`,`mlx-community/gemma-4-26b-a4b-it-8bit`,`mlx-community/gemma-4-26b-a4b-it-4bit`,`mlx-community/gemma-4-31b-it-8bit`,`mlx-community/gemma-4-31b-it-4bit`],l=process.argv.findIndex(e=>e===`--test-caps`);if(l>=0){let e=process.argv[l+1]||`ggml-llm`;e!==`ggml-llm`&&e!==`ggml-stt`&&e!==`mlx-llm`&&(console.error(`Only ggml-llm, ggml-stt, and mlx-llm backends are supported for testing capabilities`),process.exit(1));let t=process.argv.findIndex(e=>e===`--test-models`),n=process.argv.includes(`--test-models-default`);if(e===`mlx-llm`)if(t>=0){let e=process.argv[t+1];e||(console.error(`Error: --test-models requires a comma-separated list of model IDs`),process.exit(1)),await ia({modelIds:e.split(`,`).map(e=>e.trim()),defaultConfig:i})}else if(n)await ia({modelIds:c,defaultConfig:i});else{let e=process.argv.findIndex(e=>e===`--test-caps-model-id`);await aa({modelId:e>=0?process.argv[e+1]:null,defaultConfig:i})}else if(e===`ggml-stt`)if(t>=0){let e=process.argv[t+1];e||(console.error(`Error: --test-models requires a comma-separated list of model IDs`),process.exit(1)),await ra({modelIds:e.split(`,`).map(e=>e.trim()),defaultConfig:i})}else if(n)await ra({modelIds:s,defaultConfig:i});else{let e=process.argv.findIndex(e=>e===`--test-caps-model-id`);await oa({modelId:e>=0?process.argv[e+1]:null,defaultConfig:i})}else if(t>=0){let e=process.argv[t+1];e||(console.error(`Error: --test-models requires a comma-separated list of model IDs`),process.exit(1)),await ta({modelIds:e.split(`,`).map(e=>e.trim()),defaultConfig:i})}else if(n)await ta({modelIds:o,defaultConfig:i});else{let e=process.argv.findIndex(e=>e===`--test-caps-model-id`);await na({modelId:e>=0?process.argv[e+1]:null,defaultConfig:i})}}let u=Ii(i);t&&(u.server.port=t),u.server.port||(u.server.port=2080),_a({config:u,enableOpenAICompat:process.env.ENABLE_OPENAI_COMPAT_ENDPOINT===`1`||u.global.openai_compat?.enabled===!0,enableAnthropicMessages:process.env.ENABLE_ANTHROPIC_MESSAGES_ENDPOINT===`1`||u.global.anthropic_messages?.enabled===!0}).then(async({port:e,openaiEnabled:t,anthropicMessagesEnabled:n,autoDiscover:r})=>{let o=$i();console.log(`Buttress server listening on port ${e}`),console.log(`--------------------------------`),await ma(),console.log();let s={"ggml-llm":`LLM (GGML)`,"ggml-stt":`STT (GGML)`,"mlx-llm":`LLM (MLX)`};console.log(`Current supported Generators:`);let c=new Set((u?.generators||[]).map(e=>e.type).filter(Boolean));if(c.size===0)console.log(`- LLM (GGML)`),console.log(`- STT (GGML)`);else for(let e of c)console.log(`- ${s[e]||e}`);console.log(),console.log("Please configure `Buttress (Remote Inference)` in the Generator to connect to this server."),console.log(),console.log(`- Use http://${o}:${e} to connect to this server via LAN.`),console.log(`- Visit http://${o}:${e}/status to see status via LAN.`),console.log(),t?(console.log(`OpenAI-compatible API [EXPERIMENTAL]:`),console.log(`- Base URL: http://${o}:${e}/oai-compat/v1`),console.log(`- Chat completions: POST http://${o}:${e}/oai-compat/v1/chat/completions`),console.log(`- Models: GET http://${o}:${e}/oai-compat/v1/models`),console.log()):(console.log(`OpenAI-compatible API [EXPERIMENTAL]: disabled`),console.log(` Set [openai_compat] enabled = true in config to enable`),console.log()),n?(console.log(`Anthropic Messages API [EXPERIMENTAL]:`),console.log(`- Base URL: http://${o}:${e}/anthropic-messages`),console.log(`- Messages: POST http://${o}:${e}/anthropic-messages/v1/messages`),console.log(`- Count tokens: POST http://${o}:${e}/anthropic-messages/v1/messages/count_tokens`),console.log()):(console.log(`Anthropic Messages API [EXPERIMENTAL]: disabled`),console.log(` Set [anthropic_messages] enabled = true in config to enable`),console.log()),r&&(console.log(`Auto-discover enabled`),console.log()),i&&await a(i)}).catch(e=>{console.error(`Failed to start Buttress server:`,e),process.exitCode=1})};const{version:la,name:ua}=Zr(),da=async()=>{let e=`https://registry.npmjs.org/${ua}/latest`;try{let t=new AbortController,n=setTimeout(()=>t.abort(),3e3),r=await fetch(e,{headers:{Accept:`application/json`},signal:t.signal});return clearTimeout(n),r.ok&&(await r.json()).version||null}catch{return null}},fa=(e,t)=>{if(!t)return!1;let n=e.split(/[.-]/),r=t.split(/[.-]/);for(let e=0;e<Math.max(n.length,r.length);e+=1){let t=parseInt(n[e])||0,i=parseInt(r[e])||0;if(i>t)return!0;if(i<t)return!1}return!1},pa=e=>{console.log(``),console.log(`\x1B[33mâ•─────────────────────────────────────────────────╮\x1B[0m`),console.log(`\x1B[33m│\x1B[0m Update available! \x1B[2m%s\x1B[0m → \x1B[32m%s\x1B[0m`,la.padEnd(12),e.padEnd(12),`\x1B[33m│\x1B[0m`),console.log(`\x1B[33m│\x1B[0m \x1B[33m│\x1B[0m`),console.log(`\x1B[33m│\x1B[0m Run to upgrade: \x1B[33m│\x1B[0m`),console.log(`\x1B[33m│\x1B[0m \x1B[36mnpm install -g %s\x1B[0m \x1B[33m│\x1B[0m`,ua.padEnd(27)),console.log(`\x1B[33m╰─────────────────────────────────────────────────╯\x1B[0m`),console.log(``)},ma=async()=>{try{let e=await da();e&&fa(la,e)&&pa(e)}catch{}},ha=typeof process<`u`&&process.versions&&process.versions.node,ga=async({backend:e,router:r,config:i,enableOpenAICompat:o,enableAnthropicMessages:l})=>{try{await c.mkdir(i.server.temp_file_dir,{recursive:!0})}catch{}let u=$i()||`0.0.0.0`,d={id:i.server.id,name:i.server.name,version:la,address:u,port:i.server.port,url:`http://${u}:${i.server.port}`,generators:Mi(i,i.generators.map(e=>e.type)),authentication:{required:!0,type:`device-group`}},f=new n({serve:{maxRequestBodySize:i.server.max_body_size},websocket:{idleTimeout:Math.ceil(i.server.session_timeout/1e3)},adapter:ha?t():void 0}).state({sessions:new Map,backend:e||Lr,config:i,serverInfo:d});r&&f.use(r),i.autodiscover?.http?.enabled&&f.use(ni(i)),f.use(ii),f.use(li),o&&f.use(vi(i)),l&&f.use(wi(i));let p={INVALID_REQUEST:-32600,INVALID_PARAMS:-32602,METHOD_NOT_FOUND:-32601,INTERNAL_ERROR:-32603},m=e=>e.id??e.raw?.id??e.remoteAddress;return f.ws(`/buttress/rpc`,{parse:(e,t)=>{if(typeof t==`string`)try{return JSON.parse(t)}catch{return e.send(JSON.stringify({jsonrpc:`2.0`,error:{code:p.INVALID_REQUEST,message:`Invalid request`},id:null})),null}return t},body:a.Object({jsonrpc:a.String(),method:a.String(),params:a.String(),id:a.String()}),open(e){let t=m(e);if(console.log(`[Request] New connection: ${t}`),!e.data.store.sessions.has(t))e.data.store.sessions.set(t,{streamReaders:new Map,generators:new Set,initializedContexts:new Set,timeout:null});else{let n=e.data.store.sessions.get(t);n&&(clearTimeout(n.timeout),n.timeout=null)}},async message(e,{id:t,method:n,params:r}){let i=m(e);console.log(`[Request] Received request from ${i}: ${n}`);let a=e.data.store.sessions.get(i),[o,c]=n.split(`.`),l=Ji[o]?.[c];if(!l){e.send(JSON.stringify({jsonrpc:`2.0`,error:{code:p.METHOD_NOT_FOUND,message:`Method not found`},id:t}));return}try{if(n===`cancel`){a.streamReaders.has(t)&&(a.streamReaders.get(t)?.cancel(),a.streamReaders.delete(t));return}if(n===`ping`){e.send(JSON.stringify({jsonrpc:`2.0`,result:`pong`,id:t}));return}let u=Yi(r),d=qi[o]?.[c],f=d?d.parse(u):u,p=await l({...e.data.store,peerId:i,session:a},...f);if(p instanceof s){let r=p.getReader();a.streamReaders.set(t,r),e.send(JSON.stringify({jsonrpc:`2.0`,result:{type:`stream`},id:t}));try{for(;;){let{value:n,done:i}=await r.read();if(i)break;let{event:a,data:o}=n;e.send(JSON.stringify({jsonrpc:`2.0`,method:`notification/${a}`,params:Xi(o),id:t}))}e.send(JSON.stringify({jsonrpc:`2.0`,method:`notification/_end`,id:t}))}catch(r){console.error(`[RPC] Stream error for ${n}:`,r),e.send(JSON.stringify({jsonrpc:`2.0`,method:`notification/_error`,params:Xi(r),id:t}))}a.streamReaders.delete(t)}else e.send(JSON.stringify({jsonrpc:`2.0`,result:Xi(p),id:t}))}catch(r){if(r instanceof ne){e.send(JSON.stringify({jsonrpc:`2.0`,error:{code:p.INVALID_PARAMS,message:`Invalid params`,data:r.issues},id:t}));return}console.error(`[RPC] Handler error for ${n}:`,r),e.send(JSON.stringify({jsonrpc:`2.0`,error:{code:p.INTERNAL_ERROR,message:String(r)},id:t}))}},async close(e){let t=m(e);console.log(`[Request] Connection closed: ${t}`);let{backend:n,sessions:r}=e.data.store,a=r.get(t);a&&(a.streamReaders.forEach(e=>e.cancel()),a.streamReaders.clear(),a.timeout=setTimeout(()=>{r.delete(t),console.log(`[Request] Session timed out: ${t}`),a.generators.forEach(e=>{n.finalizeGenerator(e)})},i.server.session_timeout))}}),{app:f,config:i}},_a=async({backend:e,router:t,config:n,enableOpenAICompat:r=!1,enableAnthropicMessages:i=!1})=>{let{app:a,config:o}=await ga({backend:e,router:t,config:n,enableOpenAICompat:r,enableAnthropicMessages:i}),{server:{port:s}}=o,c=[new Promise(e=>a.listen(s,e))],l=null;return o.autodiscover&&(l=new Qi(o.autodiscover,()=>a.store.serverInfo),c.push(l.start())),await Promise.all(c),{app:a,port:s,openaiEnabled:r,anthropicMessagesEnabled:i,autoDiscover:l}},va=[new URL(`index.mjs`,import.meta.url).pathname,new URL(`index.ts`,import.meta.url).pathname];(process.argv[1]?.endsWith(`/bricks-buttress`)||va.includes(process.argv[1]))&&await ca();export{ma as checkAndNotifyUpdates,da as checkForUpdates,fa as compareVersions,ga as createServer,pa as logUpdateMessage,Ii as processConfig,qr as startModelDownload,_a as startServer};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fugood/buttress-server",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.25.0-beta.6",
|
|
4
4
|
"main": "lib/index.mjs",
|
|
5
5
|
"types": "lib/index.d.mts",
|
|
6
6
|
"type": "module",
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"tsdown": "^0.20.1",
|
|
45
45
|
"typescript": "^5.9.3"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "d8a3bbd695e9118cf725b7226866d804bbe56bbe"
|
|
48
48
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AnyElysia } from "elysia";
|
|
2
|
+
import * as backendCore from "@fugood/buttress-backend-core";
|
|
3
|
+
import { AutodiscoverService } from "./autodiscover";
|
|
4
|
+
import type { Config } from "./types";
|
|
5
|
+
export { startModelDownload } from "@fugood/buttress-backend-core";
|
|
6
|
+
export { processConfig } from "./utils/config";
|
|
7
|
+
export declare const checkForUpdates: () => Promise<string | null>;
|
|
8
|
+
export declare const compareVersions: (current: string, latest: string) => boolean;
|
|
9
|
+
export declare const logUpdateMessage: (latestVersion: string) => void;
|
|
10
|
+
export declare const checkAndNotifyUpdates: () => Promise<void>;
|
|
11
|
+
export type Backend = typeof backendCore;
|
|
12
|
+
export interface StartServerOptions {
|
|
13
|
+
backend?: Backend;
|
|
14
|
+
router?: AnyElysia;
|
|
15
|
+
config: Config;
|
|
16
|
+
enableOpenAICompat?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare const createServer: ({ backend, router, config, enableOpenAICompat }: StartServerOptions) => Promise<{
|
|
19
|
+
app: AnyElysia;
|
|
20
|
+
config: Config;
|
|
21
|
+
}>;
|
|
22
|
+
export declare const startServer: ({ backend, router, config, enableOpenAICompat }: StartServerOptions) => Promise<{
|
|
23
|
+
app: AnyElysia;
|
|
24
|
+
port: number;
|
|
25
|
+
openaiEnabled: boolean;
|
|
26
|
+
autoDiscover: AutodiscoverService | null;
|
|
27
|
+
}>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{createRequire as s3}from"node:module";var l3=Object.defineProperty;var i3=($,X)=>{for(var Z in X)l3($,Z,{get:X[Z],enumerable:!0,configurable:!0,set:(W)=>X[Z]=()=>W})};var P=($,X)=>()=>($&&(X=$($=0)),X);var R1=s3(import.meta.url);var w1=($,X,Z)=>Math.min(Math.max($,X),Z),F1=($)=>$?40:0,E1=($=0)=>{if(!$)return 0;return w1($/12884901888*20,0,20)},M1=($=0)=>{if(!$)return 0;return w1($/34359738368*10,0,10)},x1=($)=>$?10:0,S1=($="default",X=null)=>{let Z=String($).toLowerCase();if(!Z)return 0;if(Z.includes("cuda"))return 20;if(Z.includes("vulkan"))return 10;if(Z.includes("default"))return X==="darwin"||X==="ios"?15:5;return 0},q0=({platform:$,variant:X,hasGpu:Z,gpuUsableBytes:W=0,cpuUsableBytes:j=0,ok:V=!0}={})=>{if(!V)return 0;let Y=F1(Z)+S1(X,$)+E1(W),J=M1(j),N=x1(V);return Math.min(100,Math.round(Y+J+N))},F4=({platform:$,variant:X,hasGpu:Z,gpuUsableBytes:W=0,cpuUsableBytes:j=0,ok:V=!0}={})=>({gpuPresence:F1(Z),variant:S1(X,$),gpuMemory:E1(W),cpuMemory:M1(j),availability:x1(V)});var D1,u0=0.85,f0=0.5,P1=($)=>{if(!$&&$!==0)return[];if(Array.isArray($))return $.filter((X)=>X!=null);return[$]},n3=($)=>{if(!$)return null;return String($).trim().toLowerCase()||null},r3=({variant:$,preferVariants:X=[],variantPreference:Z=[],defaultVariants:W=D1}={})=>{let j=[];if($)j.push($);j.push(...P1(X)),j.push(...P1(Z)),j.push(...W);let V=j.map(n3).filter(Boolean);return Array.from(new Set(V))},T1=($={})=>{let X=String($.type||$.deviceType||$.kind||"").toLowerCase();if(X.includes("gpu"))return!0;if(X.includes("cuda"))return!0;if(X.includes("metal"))return!0;if(X.includes("vulkan"))return!0;if(X.includes("snapdragon"))return!0;return!1},o3=($)=>{if(!Array.isArray($))return[];return $.map((X)=>({...X}))},a3=($,X)=>{if($==="snapdragon")return X.filter((Z)=>Z.deviceName!=="GPUOpenCL");return X},k1=({platform:$,totalMemoryInBytes:X,variant:Z,devices:W,gpuMemoryFraction:j,cpuMemoryFraction:V,ok:Y,error:J})=>{let N=o3(a3(Z,W)),H=N.some(T1),O=N.filter((R)=>T1(R)&&Number.isFinite(Number(R.maxMemorySize))).reduce((R,U)=>R+U.maxMemorySize,0),q=X,_=H?Math.floor(O*j):0,G=q?Math.floor(q*V):0,w={platform:$,variant:Z,hasGpu:H,gpuUsableBytes:_,cpuUsableBytes:G,ok:Y},z=q0(w),A=Y?F4(w):null;return{platform:$,ok:Y,variant:Z,hasGpu:H,devices:N,gpuTotalBytes:O,gpuUsableBytes:_,cpuTotalBytes:q,cpuUsableBytes:G,score:z,breakdown:A,error:J,timestamp:new Date().toISOString()}},E4=({device:$,modelBytes:X=0,kvCacheBytes:Z=0}={})=>{if(!$)return{totalRequiredBytes:X+Z,fitsInGpu:!1,fitsInCpu:!1,limiting:"unknown-device"};let W=Math.max(0,Number(X)||0)+Math.max(0,Number(Z)||0),j=$.hasGpu&&W>0&&W<=$.gpuUsableBytes,V=W>0&&W<=$.cpuUsableBytes,Y="ok";if(!j&&$.hasGpu)Y="gpu-memory";if(!V)Y=j?"cpu-memory":"insufficient-memory";return{totalRequiredBytes:W,fitsInGpu:j,fitsInCpu:V,limiting:Y}},E0=async({platform:$,variant:X=null,preferVariants:Z=[],variantPreference:W=[],gpuMemoryFraction:j=u0,cpuMemoryFraction:V=f0,includeBreakdown:Y=!1,totalMemoryInBytes:J,modelBytes:N=null,kvCacheBytes:H=null,limitedKvCacheBytes:O=null,dependencies:q={},defaultVariants:_=D1}={})=>{let{getBackendDevicesInfo:G,isLibVariantAvailable:w}=q;if(typeof G!=="function"||typeof w!=="function")throw TypeError("GGML capability detection requires getBackendDevicesInfo and isLibVariantAvailable functions");let z=r3({variant:X,preferVariants:Z,variantPreference:W,defaultVariants:_}),A=[];for(let B of z)try{if(!await w(B))throw Error(`Variant ${B} not available on this platform`);let F=await G(B);A.push(k1({platform:$,totalMemoryInBytes:J,variant:B,devices:F,gpuMemoryFraction:j,cpuMemoryFraction:V,ok:!0}))}catch(L){let F=L instanceof Error?L.message:String(L);A.push(k1({platform:$,totalMemoryInBytes:J,variant:B,devices:[],gpuMemoryFraction:j,cpuMemoryFraction:V,ok:!1,error:F}))}let U=A.filter((B)=>B.ok)[0]||null,Q={ok:Boolean(U),selected:U?{...U,breakdown:Y?U.breakdown:void 0}:null,attempts:A};if(!Y&&Q.selected)delete Q.selected.breakdown;if(!Q||!N&&!H)return Q;let K=(B)=>{if(!B)return B;let L=E4({device:B,modelBytes:N||0,kvCacheBytes:H||0}),F=null;if(O!=null&&O!==H)F=E4({device:B,modelBytes:N||0,kvCacheBytes:O});return{...B,fit:L,...F&&{limitedFit:F}}};return Q.selected=K(Q.selected),Q.attempts=Array.isArray(Q.attempts)?Q.attempts.map(K):Q.attempts,Q},g0="ggml-llm";var p0=P(()=>{D1=["cuda","vulkan","snapdragon","default"]});var M4="ggml-stt",b1,x4=async({platform:$,variant:X=null,preferVariants:Z=[],variantPreference:W=[],gpuMemoryFraction:j=u0,cpuMemoryFraction:V=f0,includeBreakdown:Y=!1,totalMemoryInBytes:J,modelBytes:N=null,processingBytes:H=null,kvCacheBytes:O=null,dependencies:q={}}={})=>{let _=W&&W.length>0?W:b1;return E0({platform:$,variant:X,preferVariants:Z,variantPreference:_,gpuMemoryFraction:j,cpuMemoryFraction:V,includeBreakdown:Y,totalMemoryInBytes:J,modelBytes:N,kvCacheBytes:H??O,dependencies:q,defaultVariants:b1})};var S4=P(()=>{p0();b1=["cuda","vulkan","default"]});var t3,_0=async({platform:$,totalMemoryInBytes:X,backend:Z=g0,dependencies:W,...j}={})=>{let V=t3.get(Z);if(!V)throw Error(`No capability detector registered for backend "${Z}"`);return await V({...j,dependencies:W,totalMemoryInBytes:X,platform:$})};var v1=P(()=>{p0();S4();t3=new Map([[g0,E0],[M4,x4]])});var h1,P4=($)=>{let X=$?String($).toLowerCase():"f16";return h1[X]||h1.f16},T4=($,X,Z,W,j,V={},{totalLayers:Y=null,swaLayers:J=0,swaContext:N=null,swaContextMultiplier:H=1,swaAdditionalTokens:O=0,swaFull:q=!1}={})=>{if(!$||!X||!Z||!W||!j)return 0;let _=Y!=null&&Y!==void 0?Number(Y):Number($),G=Math.max(0,Math.floor(_));if(!G)return 0;let w=P4(V.k),z=P4(V.v),A=Number(Z)*(Number(W)*w+Number(j)*z);if(!A)return 0;let R=Math.max(0,Number(X)||0),U=Math.min(G,Math.max(0,Math.floor(Number(J)||0))),Q=Math.max(0,G-U),K=N!=null&&Number.isFinite(Number(N))?Math.max(0,Number(N)):R,B=Math.max(1,Number(H)||1),L=Math.max(0,Number(O)||0),F=K*B+L,E=q?R:Math.min(R,F),M=Q*R+U*Math.max(0,Math.floor(E));return Math.round(A*M)},m0=({modelBytes:$=0,audioLengthSeconds:X=30,sampleRate:Z=16000,bytesPerSample:W=4}={})=>{let j=Math.max(0,Number($)||0),V=Math.max(0,Math.floor(Math.max(0,X)*Z*W)),Y=1048576,J=1073741824,N;if(j<209715200)N=125829120;else if(j<524288000)N=146800640;else if(j<2147483648)N=157286400;else N=167772160;let H;if(j<209715200)H=73400320;else if(j<524288000)H=141557760;else if(j<2147483648)H=230686720;else H=230686720;let O;if(j<104857600)O=20971520;else if(j<209715200)O=31457280;else if(j<524288000)O=89128960;else if(j<2147483648)O=225443840;else O=377487360;let q=N+H+O,_=j+q+V;return{modelBytes:j,audioBufferBytes:V,processingBufferBytes:q,totalBytes:_}};var k4=P(()=>{h1={f16:2,f32:4,q8_0:1,q6_k:0.75,q5_k:0.625,q5_k_m:0.625,q5_k_s:0.625,q5_1:0.625,q5_0:0.625,q4_k:0.5,q4_k_m:0.5,q4_k_s:0.5,q4_1:0.5,q4_0:0.5,iq4_nl:0.5}});var D4=($)=>$?String($).trim().toLowerCase():null,e3=($={},X=null)=>{if(!$)return null;let Z=D4(X),W=Z?`${Z}.attention.sliding_window`:null,j=(W&&$[W]!=null?$[W]:null)??$["llama.attention.sliding_window"];if(j==null)return null;let V=Number(j);return Number.isFinite(V)?V:null},C1=($=0,X=0,Z=!1)=>{let W=Math.max(0,Math.floor(Number($)||0)),j=Math.max(0,Math.floor(Number(X)||0));if(!W||j===1)return 0;if(j<=0)return W;let V=Math.max(0,j-1),Y=Math.floor(W/j),J=W%j,N=Z?Math.max(0,J-1):Math.min(J,V);return Y*V+N},I1=({arch:$,nLayer:X=0})=>({arch:D4($),enabled:!1,window:null,pattern:null,denseFirst:!1,type:null,kvLayers:Math.max(0,Math.floor(Number(X)||0)),swaLayers:0}),$6,c0=({arch:$,metadata:X={},nLayer:Z=0}={})=>{let W=D4($||X["general.architecture"]),j=Math.max(0,Math.floor(Number(Z)||0)),V=e3(X,W),Y=W?$6.get(W):null;if(!Y)return I1({arch:W,nLayer:Z});let J=Y({nLayer:j,nSwa:V,metadata:X});if(!J||!J.enabled||!J.window||J.window<=0)return I1({arch:W,nLayer:Z});let N=Math.max(0,Math.floor(Number(J.pattern)||0)),H=J.kvLayers!=null&&Number.isFinite(Number(J.kvLayers))?Number(J.kvLayers):j,O=Math.max(0,Math.floor(H)),q=C1(O,N,Boolean(J.denseFirst));return{arch:W,enabled:q>0,window:J.window,pattern:N,denseFirst:Boolean(J.denseFirst),type:J.type||"standard",kvLayers:O,swaLayers:q}};var y1=P(()=>{$6=new Map([["llama4",({nSwa:$})=>{if($===0)return{enabled:!1};return{enabled:!0,window:$&&$>0?$:8192,pattern:4,type:"chunked"}}],["afmoe",({nSwa:$})=>{if(!$||$<=0)return{enabled:!1};return{enabled:!0,window:$,pattern:4,type:"standard"}}],["phi3",()=>({enabled:!1})],["gemma2",({nSwa:$})=>{let X=$&&$>0?$:4096;if(!X)return{enabled:!1};return{enabled:!0,window:X,pattern:2,type:"standard"}}],["gemma3",({nSwa:$})=>{if(!$||$<=0)return{enabled:!1};return{enabled:!0,window:$,pattern:6,type:"standard"}}],["gemma3n",({nLayer:$,nSwa:X})=>{if(!X||X<=0)return{enabled:!1};return{enabled:!0,window:X,pattern:5,type:"standard",kvLayers:Math.min(20,$)}}],["gemma-embedding",({nSwa:$})=>{if(!$||$<=0)return{enabled:!1};return{enabled:!0,window:$,pattern:6,type:"symmetric"}}],["cohere2",({nSwa:$})=>{if(!$||$<=0)return{enabled:!1};return{enabled:!0,window:$,pattern:4,type:"standard"}}],["olmo2",({nSwa:$})=>{if(!$||$<=0)return{enabled:!1};return{enabled:!0,window:$,pattern:4,type:"standard"}}],["exaone4",({nLayer:$,nSwa:X})=>{let Z=$>=64,W=null;if(X&&X>0)W=X;else if(Z)W=4096;if(!W)return{enabled:!1};return{enabled:!0,window:W,pattern:4,type:"standard"}}],["gpt-oss",({nSwa:$})=>{if(!$||$<=0)return{enabled:!1};return{enabled:!0,window:$,pattern:2,type:"standard"}}],["smallthinker",({nSwa:$})=>{if(!$||$<=0)return{enabled:!1};return{enabled:!0,window:4096,pattern:4,denseFirst:!0,type:"standard"}}]])});var X6,Z6,u1=($)=>$?String($).trim().toLowerCase():null,f1=($)=>{let X=u1($);return X?X6.has(X):!1},g1=($)=>{let X=u1($);return X?Z6.has(X):!1},b4=($)=>{if(f1($))return"recurrent";if(g1($))return"hybrid";return"transformer"},d0=($={})=>{let X=$["general.architecture"],Z=(F,E=null)=>{let M=$[F],x=Number(M);return Number.isFinite(x)?x:E},W=(F,E=null)=>{let M=$[F];if(Array.isArray(M))return M;let x=Number(M);return Number.isFinite(x)?x:E},j=X?Z(`${X}.context_length`,Z("llama.context_length")):null,V=X?Z(`${X}.block_count`,Z("llama.block_count")):null,Y=X?Z(`${X}.embedding_length`,Z("llama.embedding_length")):null,J=X?Z(`${X}.attention.head_count`,Z("llama.attention.head_count")):null,N=X?W(`${X}.attention.head_count_kv`,W("llama.attention.head_count_kv",J)):null,H=null,O=null;if(Array.isArray(N)){let F=N.filter((E)=>Number(E)>0);if(F.length>0)H=Math.max(...F.map(Number)),O=F.length;else H=0,O=0}else H=N;let q=X?Z(`${X}.attention.key_length`,Z("llama.attention.key_length")):null,_=X?Z(`${X}.attention.value_length`,Z("llama.attention.value_length")):null,G=$["general.quantization_version"]||null,w=$["general.file_type"]||null,z=X?Z(`${X}.ssm.conv_kernel`):null,A=X?Z(`${X}.ssm.state_size`):null,R=X?Z(`${X}.ssm.inner_size`):null,U=X?Z(`${X}.ssm.group_count`):null,Q=X?Z(`${X}.ssm.time_step_rank`):null,K=X?Z(`${X}.rwkv.head_size`):null,B=X?Z(`${X}.rwkv.token_shift_count`,2):null,L=O!=null&&V!=null?V-O:null;return{arch:X,nCtxTrain:j,nLayer:V,nEmbd:Y,nHead:J,nHeadKv:H,nEmbdHeadK:q,nEmbdHeadV:_,quantVersion:G,fileType:w,attentionLayerCount:O,recurrentLayerCount:L,ssmDConv:z,ssmDState:A,ssmDInner:R,ssmNGroup:U,ssmDtRank:Q,rwkvHeadSize:K,rwkvTokenShiftCount:B}},G0=({layerCount:$,headKvCount:X,embdHeadKCount:Z,embdHeadVCount:W,cacheTypes:j,swaConfig:V,kvUnified:Y=!1,nParallel:J=1,swaFull:N=!1,arch:H=null,attentionLayerCount:O=null})=>{let q=b4(H);if(q==="recurrent")return()=>0;let _=q==="hybrid"&&O!=null?Math.max(0,Math.floor(Number(O)||0)):$,G=V?.window&&Y?Math.max(1,Number(J)||1):1,w=Y?1:Math.max(1,Number(J)||1);return(z)=>T4(_,z,X,Z,W,j,{totalLayers:_,swaLayers:V?.swaLayers||0,swaContext:V?.window,swaFull:N,swaContextMultiplier:G})*w},l0=({nLayer:$,nEmbd:X,recurrentLayerCount:Z=null,nSeqMax:W=1,ssmDConv:j=null,ssmDState:V=null,ssmDInner:Y=null,ssmNGroup:J=null,ssmDtRank:N=null,rwkvHeadSize:H=null,rwkvTokenShiftCount:O=2,arch:q=null})=>{if(b4(q)==="transformer")return 0;let G=Z!=null?Math.max(0,Math.floor(Number(Z)||0)):Math.max(0,Math.floor(Number($)||0));if(G===0)return 0;let w=Math.max(1,Math.floor(Number(W)||1)),z=0,A=0;if(H!=null&&H>0&&X!=null&&X>0)z=Math.max(1,Number(O)||2)*X,A=X*H;else if(V!=null&&Y!=null){let Q=Math.max(0,Number(j)||0),K=Math.max(0,Number(V)||0),B=Math.max(0,Number(Y)||0),L=Math.max(1,Number(J)||1);if(Math.max(0,Number(N)||0)>0)z=Q>0?(Q-1)*2*L*K:0,A=Math.floor(K*B/2);else z=Q>0?(Q-1)*(B+2*L*K):0,A=K*B}else return 0;let R=4,U=(z+A)*w*G*R;return Math.max(0,U)},i0=({maxCtx:$,availableMemory:X,modelBytes:Z,kvBytesForCtx:W})=>{let j=Math.max(1,Math.floor(Number($)||0));if(!W||X<=Z)return j;let V=1,Y=j,J=j;while(V<=Y){let N=Math.floor((V+Y)/2);if(Z+W(N)<=X)J=N,V=N+1;else Y=N-1}return J};var p1=P(()=>{k4();X6=new Set(["mamba","mamba2","rwkv6","rwkv6qwen2","rwkv7","arwkv7"]),Z6=new Set(["jamba","falcon-h1","plamo2","granitehybrid","lfm2","lfm2moe","nemotron_h","nemotron_h_moe","qwen3next"])});var M0=P(()=>{v1();k4();p0();S4();y1();p1()});import{EventEmitter as W6}from"node:events";class h4{constructor($=j6){this.maxEntries=$,this.modelLoads=[],this.completions=[],this.transcriptions=[]}addModelLoad($){v4(this.modelLoads,$,this.maxEntries),e.emit("status:modelLoad",$),e.emit("status:change",{type:"modelLoad",entry:$})}addCompletion($){v4(this.completions,$,this.maxEntries),e.emit("status:completion",$),e.emit("status:change",{type:"completion",entry:$})}addTranscription($){v4(this.transcriptions,$,this.maxEntries),e.emit("status:transcription",$),e.emit("status:change",{type:"transcription",entry:$})}getModelLoadHistory(){return[...this.modelLoads].reverse()}getCompletionHistory(){return[...this.completions].reverse()}getTranscriptionHistory(){return[...this.transcriptions].reverse()}clear(){this.modelLoads=[],this.completions=[],this.transcriptions=[]}}function I4($){let X=(Z)=>$(Z);return e.on("status:change",X),()=>e.off("status:change",X)}function c1($){m1+=1;let X=m1,Z=I4($);return{subscriberId:X,unsubscribe:Z}}function C4($){let X=[];return{generators:Array.from($.entries()).filter(([,W])=>W.type==="ggml-llm").map(([W,j])=>{let{instance:V}=j,Y=[];if(V.contexts)Y=Array.from(V.contexts.entries()).map(([J,N])=>{let H={key:J,refCount:N.refCount,hasModel:Boolean(N.context)},O=N.context.parallel.getStatus();return H.parallelStatus=O,X.push({generatorId:W,contextKey:J,...O}),H});return{id:W,type:j.type,refCount:j.refCount,repoId:V.info?.model?.repoId||null,quantization:V.info?.model?.quantization||null,variant:V.info?.runtime?.variant||null,nCtx:V.info?.runtime?.n_ctx||null,nParallel:V.info?.runtime?.n_parallel||null,contexts:Y}}),parallelStatuses:X,history:{modelLoads:$0.getModelLoadHistory(),completions:$0.getCompletionHistory()}}}function y4($){return{generators:Array.from($.entries()).filter(([,Z])=>Z.type==="ggml-stt").map(([Z,W])=>{let{instance:j}=W,V=j.getStatus?.()||{},Y=V.queueStatus||{processing:!1,queuedCount:0};return{id:Z,type:W.type,refCount:W.refCount,repoId:j.info?.model?.repoId||null,quantization:j.info?.model?.quantization||null,modelType:j.info?.model?.modelType||null,variant:j.info?.runtime?.variant||null,hasContext:V.hasContext||!1,contextRefCount:V.contextRefCount||0,queueStatus:Y}}),history:{modelLoads:N0.getModelLoadHistory(),transcriptions:N0.getTranscriptionHistory()}}}function d1($){return{timestamp:new Date().toISOString(),ggmlLlm:C4($),ggmlStt:y4($)}}var j6=9999,e,v4=($,X,Z)=>{if($.push({...X,timestamp:X.timestamp||new Date().toISOString()}),$.length>Z)$.shift()},$0,N0,m1=0;var s0=P(()=>{e=new W6;e.setMaxListeners(100);$0=new h4,N0=new h4});import y from"node:path";import T0 from"node:os";import{stat as B0,mkdir as Y6,open as Q6,unlink as o,readFile as t1,writeFile as e1,rename as f4,readdir as J6}from"node:fs/promises";import{createHash as p4}from"node:crypto";import{gguf as V6}from"@huggingface/gguf";import{loadModel as N6,getBackendDevicesInfo as $2,isLibVariantAvailable as X2}from"@fugood/llama.node";import U6 from"bytes";import*as G6 from"node:stream/web";class L0{constructor($,X){this.config=$,this.plan=X,this.baseDir=$.runtime.cache_dir,this.enabled=$.runtime.session_cache?.enabled!==!1,this.maxSizeBytes=x6($.runtime.session_cache?.max_size_bytes,10737418240),this.maxEntries=$.runtime.session_cache?.max_entries||1000,this.metadata={variant:X.info?.runtime?.variant||null,n_gpu_layers:X.info?.runtime?.n_gpu_layers||0,n_ctx:X.info?.runtime?.n_ctx||0,modelPath:X.localPath,cacheTypeK:X.info?.runtime?.cache_type_k||"f16",cacheTypeV:X.info?.runtime?.cache_type_v||"f16",kvUnified:X.info?.runtime?.kv_unified??null,swaFull:X.info?.runtime?.swa_full??null,flashAttnType:X.info?.runtime?.flash_attn_type||"off",isRecurrent:!1,isHybrid:!1},this.cacheMap=null,this.initialized=!1}updateModelInfo($){if(!$)return;if(this.metadata.isRecurrent=Boolean($.is_recurrent),this.metadata.isHybrid=Boolean($.is_hybrid),this.metadata.isRecurrent||this.metadata.isHybrid)console.log(`[SessionCache] Model architecture: recurrent=${this.metadata.isRecurrent}, hybrid=${this.metadata.isHybrid}`)}requiresExactMatch(){return this.metadata.isRecurrent||this.metadata.isHybrid}static checkTokenPrefixMatch($,X){if($.length>X.length)return!1;for(let Z=0;Z<$.length;Z+=1)if($[Z]!==X[Z])return!1;return!0}static async tokenizeToArray($,X){let Z=await $.tokenize(X);return Array.from(Z?.tokens||[])}async findFormattedMatchForRecurrent($,X,Z){let W=await L0.tokenizeToArray(Z,X),j=$.map(async(_)=>{try{let G=await L0.tokenizeToArray(Z,_.fullText);if(L0.checkTokenPrefixMatch(G,W))return{entry:_,usePromptState:!1,tokenCount:G.length};if(_.promptStatePath&&_.promptText){let w=await L0.tokenizeToArray(Z,_.promptText);if(L0.checkTokenPrefixMatch(w,W))return{entry:_,usePromptState:!0,tokenCount:w.length}}return null}catch(G){return console.warn(`[SessionCache] Failed to check entry ${_.id}: ${G.message}`),null}}),Y=(await Promise.all(j)).find((_)=>_!==null);if(!Y)return console.log("[SessionCache] No token prefix match found for recurrent/hybrid model"),null;let{entry:J,usePromptState:N,tokenCount:H}=Y;console.log(`[SessionCache] Token prefix match: ${J.id} (${H} tokens, usePromptState=${N})`);let O=N?J.promptStatePath:J.stateFilePath;if(!await n1(O))return await this.removeStaleEntry(J),null;return J.lastAccessedAt=new Date().toISOString(),await x0(this.cacheMap,this.baseDir).catch(()=>{}),{entry:J,usePromptState:N}}async initialize(){if(!this.enabled||this.initialized)return;try{await H0($4(this.baseDir)),await H0(o0(this.baseDir)),await H0(U2(this.baseDir)),this.cacheMap=await A6(this.baseDir),this.initialized=!0,console.log(`[SessionCache] Initialized with ${Object.keys(this.cacheMap.entries).length} entries`)}catch($){console.warn(`[SessionCache] Failed to initialize: ${$.message}`),this.enabled=!1}}async removeStaleEntry($){if(console.log(`[SessionCache] Removing stale entry: ${$.id}`),$.stateFilePath)await o($.stateFilePath).catch(()=>{});if($.promptStatePath)await o($.promptStatePath).catch(()=>{});delete this.cacheMap.entries[$.id],this.cacheMap.totalSize-=($.stateFileSize||0)+($.promptStateSize||0),await x0(this.cacheMap,this.baseDir).catch(()=>{})}async findMatchingEntry($,X=null){if(!this.enabled||!this.cacheMap)return null;let Z=this.requiresExactMatch();if(Z&&X){let J=Object.values(this.cacheMap.entries).filter((N)=>m4(N.metadata,this.metadata)&&N.fullText);return this.findFormattedMatchForRecurrent(J,$,X)}let W=w6($,this.metadata,this.cacheMap,Z);if(!W)return null;let{entry:j}=W;if(!await n1(j.stateFilePath))return await this.removeStaleEntry(j),null;return j.lastAccessedAt=new Date().toISOString(),await x0(this.cacheMap,this.baseDir).catch(()=>{}),{entry:j,usePromptState:!1}}async prepareCompletionOptions($,X,Z=null){let W={options:$,cacheEntry:null,promptPrefix:null};if(!this.enabled)return W;let j=await this.findMatchingEntry(X,Z);if(!j)return W;let{entry:V,usePromptState:Y}=j,J=Y?V.promptStatePath:V.stateFilePath,N=Y?V.promptText:V.fullText;return console.log(`[SessionCache] Found matching entry: ${V.id} (${N.length} chars, usePromptState=${Y})`),{options:{...$,load_state_path:J},cacheEntry:V,promptPrefix:N}}async saveCompletionState($,X,Z,W=0,j=null){if(!this.enabled)return null;let V=$+X,Y=z6(V,this.metadata),J=()=>{if(Z)o(Z).catch(()=>{});if(j)o(j).catch(()=>{})};if(this.cacheMap.entries[Y]){console.log(`[SessionCache] Entry already exists for prompt: ${Y}, updating position`);let O=this.cacheMap.entries[Y];return O.lastAccessedAt=new Date().toISOString(),delete this.cacheMap.entries[Y],this.cacheMap.entries[Y]=O,await x0(this.cacheMap,this.baseDir).catch(()=>{}),J(),O}let N=s1(Y,this.baseDir),H=j?s1(`${Y}-prompt`,this.baseDir):null;try{await H0(y.dirname(N)),await f4(Z,N);let O=await B0(N),q=0;if(j&&H)try{await f4(j,H),q=(await B0(H)).size,console.log(`[SessionCache] Saved prompt state: ${H}`)}catch(G){console.warn(`[SessionCache] Failed to save prompt state: ${G.message}`)}let _={id:Y,promptText:$,completionText:X,fullText:V,promptTokenCount:W,stateFilePath:N,stateFileSize:O.size,promptStatePath:H||null,promptStateSize:q,metadata:{...this.metadata},createdAt:new Date().toISOString(),lastAccessedAt:new Date().toISOString()};if(this.cacheMap.entries[Y]=_,this.cacheMap.totalSize+=O.size+q,!this.requiresExactMatch())await E6(this.cacheMap,$,Y,this.metadata);return await F6(this.cacheMap,this.maxSizeBytes,this.maxEntries),await x0(this.cacheMap,this.baseDir),console.log(`[SessionCache] Saved entry: ${Y} (${O.size} bytes, ${V.length} chars)`),_}catch(O){return console.warn(`[SessionCache] Failed to save state: ${O.message}`),J(),null}}async generateTempStatePath(){return await H0(o0(this.baseDir)),B6(this.baseDir)}async cleanup(){await M6(this.baseDir)}}async function K2($,X,Z={}){let{globalDownloadManager:W=null}=Z,j=e0(X),V=await S6(j),Y=new L0(j,V);await Y.initialize();let J={id:$,type:"ggml-llm",config:j,plan:V,info:V.info,contexts:new Map,downloads:new Map,globalDownloadManager:W,sessionCache:Y,finalized:!1},N=async()=>{if(J.finalized)return;J.finalized=!0;let Q=Array.from(J.contexts.values()),K=Q.map((L)=>{if(L.released)return Promise.resolve(!1);if(L.releaseRequested||L.releaseTimer)return Promise.resolve(!1);if(L.refCount=Math.max(0,L.refCount-1),L.refCount>0)return Promise.resolve(!1);return r0(J,L)});if(await Promise.allSettled(K),Q.length===0||Q.every((L)=>L.released))await J.sessionCache.cleanup()},H=async(Q={})=>{let{onProgress:K}=Q,B=await D6(J,K);return J.sessionCache.updateModelInfo(B.modelInfo),{modelInfo:B.modelInfo?{...B.modelInfo}:null,runtime:{...J.plan.info.runtime},download:{...J.plan.info.download}}},O=async()=>{if(J.finalized)return!1;let Q=z0(J),K=J.contexts.get(Q);if(!K)return!1;return b6(J,K,!1)},q=async(Q={})=>{let{options:K={},useCache:B=!0}=Q,L=z0(J),F=J.contexts.get(L);if(!F)throw Error(`Context "${L}" not initialized`);await F.ready;let E=K.prompt||"",M=null,x=null;if(!E&&K.messages){({messages:M}=K),x={chatTemplate:K.chat_template||K.chatTemplate,jinja:K.jinja??!0,tools:K.tools,parallel_tool_calls:K.parallel_tool_calls,tool_choice:K.tool_choice,reasoning_format:K.reasoning_format,enable_thinking:K.enable_thinking,add_generation_prompt:K.add_generation_prompt,now:K.now,chat_template_kwargs:K.chat_template_kwargs};let I=await F.context.getFormattedChat(M,x.chatTemplate,x);E=I?.prompt||I||""}if(B&&J.sessionCache.enabled&&E){let{options:I}=await J.sessionCache.prepareCompletionOptions(K,E,F.context),b=await J.sessionCache.generateTempStatePath(),r=(await F.context.tokenize(E))?.tokens?.length||0,k={...I,save_state_path:b},d=J.sessionCache.requiresExactMatch(),v=Boolean(k.load_state_path),S=null;if(d&&!v)S=await J.sessionCache.generateTempStatePath(),k.save_prompt_state_path=S;let C={repoId:J.plan.info.model?.repoId||null,quantization:J.plan.info.model?.quantization||null,variant:J.plan.info.runtime?.variant||null};return T6(F.context,k,J.sessionCache,E,b,r,J.id,C,S)}let D={repoId:J.plan.info.model?.repoId||null,quantization:J.plan.info.model?.quantization||null,variant:J.plan.info.runtime?.variant||null};return P6(F.context,K,J.id,D)},_=async(Q={})=>{let{text:K="",params:B={}}=Q,L=z0(J),F=J.contexts.get(L);if(!F)throw Error(`Context "${L}" not initialized`);await F.ready;let E=await F.context.tokenize(K,B);if(!E)return{tokens:[]};let M=Array.from(E.tokens??[]).map((x)=>Number(x));return{...E,tokens:M}},G=async(Q={})=>{let{tokens:K=[]}=Q,B=z0(J),L=J.contexts.get(B);if(!L)throw Error(`Context "${B}" not initialized`);await L.ready;let F=K.map((E)=>Number(E));return L.context.detokenize(F)},w=async(Q={})=>{let{messages:K=[],template:B,params:L}=Q,F=z0(J),E=J.contexts.get(F);if(!E)throw Error(`Context "${F}" not initialized`);return await E.ready,await E.context.getFormattedChat(K,B,L)},z=()=>Array.from(J.contexts.values()).some((Q)=>!Q.released&&(Q.releaseRequested||Q.releaseTimer||Q.refCount>0)),A=()=>{J.finalized=!1},R=()=>{let Q=[],K=Array.from(J.contexts.entries()).map(([B,L])=>{let F={key:B,refCount:L.refCount,hasModel:Boolean(L.context)},E=L.context.parallel.getStatus();return F.parallelStatus=E,Q.push({contextKey:B,...E}),F});return{id:J.id,type:J.type,repoId:J.plan.info.model?.repoId||null,quantization:J.plan.info.model?.quantization||null,variant:J.plan.info.runtime?.variant||null,nCtx:J.plan.info.runtime?.n_ctx||null,nParallel:J.plan.info.runtime?.n_parallel||null,contexts:K,parallelStatuses:Q}},U=(Q)=>{let K=Array.from(J.contexts.entries()).map(([B,L])=>L.context.parallel.subscribeToStatus((F)=>{Q({contextKey:B,...F})}));return{remove:()=>{K.forEach((B)=>{if(B?.remove)B.remove()})}}};return{id:$,type:"ggml-llm",info:V.info,contexts:J.contexts,initContext:H,completion:q,tokenize:_,detokenize:G,applyChatTemplate:w,releaseContext:O,finalize:N,getStatus:R,subscribeParallelStatus:U,hasPendingReleases:z,resetFinalized:A}}async function q2($,X,Z={}){let{onProgress:W,onComplete:j,onError:V}=Z;try{let Y=e0($),J=await c4(Y),N=H2(Y,J),{repoId:H}=J;if(await P0(N,J.size)){if(console.log(`[Download] Model already exists: ${H} at ${N}`),typeof j==="function")j({localPath:N,repoId:H,alreadyExists:!0});return{started:!1,localPath:N,repoId:H,alreadyExists:!0}}let q=X.getDownload(N);if(q)return console.log(`[Download] Already downloading: ${H}`),q.then(()=>{if(typeof j==="function")j({localPath:N,repoId:H,joinedExisting:!0})}).catch((G)=>{if(typeof V==="function")V(G)}),{started:!1,localPath:N,repoId:H,alreadyDownloading:!0};console.log(`[Download] Starting download: ${H}`);let _=(async()=>{try{if(J.isSplit&&J.splitCount>0){let G=/-(\d{5})-of-(\d{5})\.gguf$/,w=y.dirname(N),z=J.splitCount,A=0;for(let R=1;R<=z;R+=1){let U=String(R).padStart(5,"0"),Q=J.filename.replace(G,`-${U}-of-${String(z).padStart(5,"0")}.gguf`),K=`${Y.model.base_url.replace(/\/+$/,"")}/${J.repoId}/resolve/${J.revision}/${Q}`,B=y.join(w,Q);if(!await P0(B))await a0(K,J.headers,B,null,(F)=>{if(F>=0&&Number.isFinite(F)){let E=(A+F)/z;if(console.log(`[Download] ${H}: ${Math.round(E*100)}%`),typeof W==="function")W(E)}});A+=1}}else await a0(J.url,J.headers,N,J.size,(G)=>{if(G>=0&&Number.isFinite(G)){if(console.log(`[Download] ${H}: ${Math.round(G*100)}%`),typeof W==="function")W(G)}});if(console.log(`[Download] Completed: ${H}`),typeof j==="function")j({localPath:N,repoId:H})}catch(G){if(console.error(`[Download] Failed: ${H}`,G.message),typeof V==="function")V(G);throw G}finally{X.deleteDownload(N)}})();return X.setDownload(N,_),{started:!0,localPath:N,repoId:H}}catch(Y){if(console.error("[Download] Failed to start download:",Y.message),typeof V==="function")V(Y);return{started:!1,localPath:null,repoId:null,error:Y.message}}}async function v6($){let X=e0($),Z=await c4(X),W=await G2(Z.url,Z.headers,X.runtime.cache_dir),{arch:j,nCtxTrain:V,nLayer:Y,nEmbd:J,nHead:N,nHeadKv:H,nEmbdHeadK:O,nEmbdHeadV:q,quantVersion:_,fileType:G,attentionLayerCount:w,recurrentLayerCount:z,ssmDConv:A,ssmDState:R,ssmDInner:U,ssmNGroup:Q,ssmDtRank:K,rwkvHeadSize:B,rwkvTokenShiftCount:L}=d0(W),F=Number.isFinite(Number(Y))?Number(Y):0,E=Number.isFinite(Number(J))?Number(J):0,M=Number.isFinite(Number(N))?Number(N):0,x=Number.isFinite(Number(H))?Number(H):M,D=M>0&&E>0?E/M:128,I=O!=null&&Number.isFinite(Number(O))?Number(O):D,b=q!=null&&Number.isFinite(Number(q))?Number(q):D,u=c0({arch:j,metadata:W,nLayer:F}),r=u&&Number.isFinite(Number(u.kvLayers))?Number(u.kvLayers):F,k=Math.max(0,Math.floor(Number(r)||0)),v=(X.model.n_ctx?Number(X.model.n_ctx):null)||V||4096,S={k:X.model.cache_type_k,v:X.model.cache_type_v},C=Z.size>0?Z.size:0,h=X.model.n_parallel||4,p=G0({layerCount:k,headKvCount:x,embdHeadKCount:I,embdHeadVCount:b,cacheTypes:S,swaConfig:u,kvUnified:X.model.kv_unified,nParallel:h,swaFull:X.model.swa_full,arch:j,attentionLayerCount:w}),f=l0({nLayer:F,nEmbd:E,recurrentLayerCount:z,nSeqMax:h,ssmDConv:A,ssmDState:R,ssmDInner:U,ssmNGroup:Q,ssmDtRank:K,rwkvHeadSize:B,rwkvTokenShiftCount:L,arch:j}),s=X.backend?.gpu_memory_fraction!=null?Math.min(1,Math.max(0,Number(X.backend.gpu_memory_fraction))):S0.backend.gpu_memory_fraction||1,W0=X.backend?.cpu_memory_fraction!=null?Math.min(1,Math.max(0,Number(X.backend.cpu_memory_fraction))):t0,A0=p(v),l=await O2(X,{modelBytes:C,kvCacheBytes:A0}),F0=(l.selected.totalMemory||0)*s,A4=Math.max(0,T0.totalmem()*W0),h0=l.selected.hasGpu?F0:A4,I0=i0({maxCtx:v,availableMemory:h0,modelBytes:C,kvBytesForCtx:p}),U0=p(v),C0=p(I0);return{kvInfo:{nCtxTrain:V,nLayer:F,nEmbd:E,nHeadKv:x,nEmbdHeadK:I,nEmbdHeadV:b,nHeadCount:M,nHeadKvCount:x,kvLayerCount:k,swa:u?.enabled?{window:u.window,pattern:u.pattern,denseFirst:u.denseFirst,type:u.type,layers:u.swaLayers}:null},modelBytes:C,kvCacheBytes:U0,limitedKvCacheBytes:C0,memoryLimitedCtx:I0,recurrentMemoryBytes:f,quantization:{name:Z.quantization||null,fileType:G,version:_}}}async function _2($=null,X={}){let{threshold:Z=1.1,includeBreakdown:W=!1,config:j,...V}=X,Y=null,J=null,N=null,H=null,O=null,q=null,_=null;if(j)try{let{modelBytes:B,kvCacheBytes:L,limitedKvCacheBytes:F,memoryLimitedCtx:E,recurrentMemoryBytes:M,kvInfo:x,quantization:D}=await v6(j);Y=B,J=L,N=F,H=E,O=M,q=x,_=D}catch(B){}let G=j?.backend?.gpu_memory_fraction!=null?Math.min(1,Math.max(0,Number(j.backend.gpu_memory_fraction))):void 0,w=j?.backend?.cpu_memory_fraction!=null?Math.min(1,Math.max(0,Number(j.backend.cpu_memory_fraction))):void 0,z=await _0({...V,platform:process.platform,totalMemoryInBytes:T0.totalmem(),backend:"ggml-llm",includeBreakdown:W,gpuMemoryFraction:G,cpuMemoryFraction:w,dependencies:{getBackendDevicesInfo:$2,isLibVariantAvailable:X2},modelBytes:Y,kvCacheBytes:J,limitedKvCacheBytes:N}),A=z.selected,R=a1(A);A.modelBytes=Y||null,A.kvCacheBytes=J||null,A.memoryLimitedCtx=H||null,A.limitedKvCacheBytes=N||null,A.recurrentMemoryBytes=O||null,A.kvInfo=q||null,A.quantization=_||null;let U=null,Q=null;if($){let B=a1($);Q={...$,score:B};let L="buttress",F="buttress-higher-score";if(!z.ok)L="local",F="buttress-unavailable";else if(!B&&B!==0)L="buttress",F="missing-client-score";else{let{fit:E,limitedFit:M}=Q,x=A?.fit,D=A?.limitedFit,I=E?.fitsInGpu||E?.fitsInCpu||M?.fitsInGpu||M?.fitsInCpu,b=x?.fitsInGpu||x?.fitsInCpu||D?.fitsInGpu||D?.fitsInCpu;if(I&&!b)L="local",F="client-fits-in-memory";else if(b&&!I)L="buttress",F="buttress-fits-in-memory";else if(B>R*Z)L="local",F="client-better";else if(R>B*Z)L="buttress",F="buttress-better";else L="either",F="comparable-scores"}U={buttressScore:R,clientScore:B,threshold:Z,recommendation:L,reason:F}}if(!z.ok&&!U)U={buttressScore:R,clientScore:$?.score??null,threshold:Z,recommendation:"local",reason:"buttress-unavailable"};let K=null;if(j)K={repoId:j.model?.repo_id||null,quantization:j.model?.quantization||null,nCtx:j.model?.n_ctx||null,cacheKType:j.model?.cache_type_k||"f16",cacheVType:j.model?.cache_type_v||"f16"};return{type:"ggml-llm",timestamp:new Date().toISOString(),buttress:z,client:Q,comparison:U,modelConfig:K}}var H6=()=>{if(typeof globalThis<"u"&&globalThis.ReadableStream&&globalThis.WritableStream)return{ReadableStream:globalThis.ReadableStream,WritableStream:globalThis.WritableStream};return G6},O6,Z2,K6,W2=($={},X={})=>{return Object.entries(X||{}).forEach(([Z,W])=>{if(W&&typeof W==="object"&&!Array.isArray(W)){if(!$[Z]||typeof $[Z]!=="object")$[Z]={};W2($[Z],W)}else $[Z]=W}),$},q6=".gguf",j2="https://huggingface.co",Y2="https://huggingface.co/api",n,Q2,t0=0.5,S0,u4=($,X=[])=>{if(!$&&$!==0)return[...X];if(Array.isArray($))return $.filter((Z)=>Z!=null);return[$]},n0=($)=>{if(!$)return null;let X=String($).toLowerCase();if(["cuda","vulkan","snapdragon","default"].includes(X))return X;return null},e0=($={})=>{let X=JSON.parse(JSON.stringify(S0));if(W2(X,$),X.backend.variant=n0(X.backend.variant),X.backend.variant_preference=Array.from(new Set(u4(X.backend.variant_preference).map(n0).filter(Boolean))),X.backend.variant_preference.length===0)X.backend.variant_preference=["cuda","vulkan","snapdragon","default"];if(X.runtime.prefer_variants=Array.from(new Set(u4(X.runtime.prefer_variants).map(n0).filter(Boolean))),X.model.preferred_quantizations=Array.from(new Set(u4(X.model.preferred_quantizations||X.model.quantizations).map((Z)=>Z?String(Z).toLowerCase():null).filter(Boolean))),X.model.quantization){let Z=String(X.model.quantization).toLowerCase();if(!X.model.preferred_quantizations.includes(Z))X.model.preferred_quantizations.unshift(Z)}return X.model.n_parallel=Math.max(1,Number(X.model.n_parallel)||4),X.model.n_batch=Math.max(1,Number(X.model.n_batch)||512),X.model.base_url=X.model.base_url||j2,X.model.api_base=X.model.api_base||Y2,X.runtime.cache_dir=X.runtime.cache_dir?y.resolve(X.runtime.cache_dir):n,X.runtime.session_cache={...S0.runtime.session_cache,...X.runtime.session_cache||{}},X.runtime.context_release_delay_ms=Math.max(0,Number(X.runtime.context_release_delay_ms)||S0.runtime.context_release_delay_ms),X},l1=($)=>{let X=$.toLowerCase();return Q2.find((W)=>X.includes(W))||null},_6=($)=>{let X=[];if($.backend.variant)X.push($.backend.variant);if($.runtime.prefer_variants.length>0)X.push(...$.runtime.prefer_variants);return X.push(...$.backend.variant_preference),X.push("default"),Array.from(new Set(X.map(n0).filter(Boolean)))},H0=async($)=>{await Y6($,{recursive:!0})},L6=($=n)=>y.join($,".metadata-cache"),J2=($,X,Z=n)=>{let W=p4("sha256").update($).digest("hex");return y.join(L6(Z),X,`${W}.json`)},V2=async($,X,Z=n)=>{try{let W=J2($,X,Z),j=await t1(W,"utf-8");return console.log(`[Cache] Hit ${X} cache:`,y.basename(W)),JSON.parse(j,(V,Y)=>{if(typeof Y==="string"&&Y.startsWith("__bigint__"))return BigInt(Y.slice(10));return Y})}catch(W){return null}},g4=async($,X,Z,W=n)=>{try{let j=J2($,X,W);await H0(y.dirname(j)),await e1(j,JSON.stringify(Z,(V,Y)=>{if(typeof Y==="bigint")return`__bigint__${Y.toString()}`;return Y}),"utf-8"),console.log(`[Cache] Wrote ${X} cache:`,y.basename(j))}catch(j){console.warn(`[Cache] Failed to write ${X} cache:`,j.message)}},$4=($=n)=>y.join($,".session-state-cache"),N2=($=n)=>y.join($4($),"cache-map.json"),o0=($=n)=>y.join($4($),"temp"),U2=($=n)=>y.join($4($),"states"),i1=()=>({version:1,entries:{},totalSize:0}),A6=async($=n)=>{try{let X=N2($),Z=await t1(X,"utf-8"),W=JSON.parse(Z);if(!W.entries||typeof W.entries!=="object")return i1();return W}catch{return i1()}},x0=async($,X=n)=>{let Z=N2(X),W=`${Z}.tmp.${Date.now()}`;try{await H0(y.dirname(Z)),await e1(W,JSON.stringify($,null,2),"utf-8"),await f4(W,Z)}catch(j){throw await o(W).catch(()=>{}),j}},z6=($,X)=>{let Z=JSON.stringify({text:$,model:X.modelPath,variant:X.variant,n_gpu_layers:X.n_gpu_layers,n_ctx:X.n_ctx,cacheTypeK:X.cacheTypeK,cacheTypeV:X.cacheTypeV,kvUnified:X.kvUnified,swaFull:X.swaFull,flashAttnType:X.flashAttnType});return p4("sha256").update(Z).digest("hex").slice(0,24)},s1=($,X=n)=>y.join(U2(X),`${$}.bin`),B6=($=n)=>{let X=`${Date.now()}-${Math.random().toString(36).slice(2,10)}`;return y.join(o0($),`${X}.bin`)},m4=($,X)=>$.modelPath===X.modelPath&&$.variant===X.variant&&$.n_gpu_layers===X.n_gpu_layers&&$.n_ctx>=X.n_ctx&&$.cacheTypeK===X.cacheTypeK&&$.cacheTypeV===X.cacheTypeV&&$.kvUnified===X.kvUnified&&$.swaFull===X.swaFull&&$.flashAttnType===X.flashAttnType&&Boolean($.isRecurrent)===Boolean(X.isRecurrent)&&Boolean($.isHybrid)===Boolean(X.isHybrid),R6=($,X)=>{let Z=Math.min($.length,X.length),W=0;while(W<Z&&$[W]===X[W])W+=1;return W},w6=($,X,Z,W=!1)=>{let j=Object.values(Z.entries);console.log(`[SessionCache] Finding match for promptText (${$.length} chars), exactMatch=${W}`),console.log(`[SessionCache] Checking ${j.length} cache entries`);let V=j.filter((J)=>m4(J.metadata,X));if(W){let J=V.find((N)=>N.fullText===$);if(J)return console.log(`[SessionCache] Exact match found: ${J.id} (${J.fullText.length} chars)`),{entry:J,prefixLength:J.fullText.length,exactMatch:!0};return null}let Y=V.reduce((J,N)=>{let H=R6($,N.fullText);if(H>J.prefixLen)return{entry:N,prefixLen:H};if(H===J.prefixLen&&N.fullText.length>(J.entry?.fullText?.length||0))return{entry:N,prefixLen:H};return J},{entry:null,prefixLen:0});if(Y.entry)return console.log(`[SessionCache] Prefix match found: ${Y.entry.id} (${Y.prefixLen}/${Y.entry.fullText.length} chars)`),{entry:Y.entry,prefixLength:Y.prefixLen};return console.log("[SessionCache] No match found"),null},F6=async($,X,Z)=>{let W=Object.values($.entries).sort((J,N)=>new Date(J.lastAccessedAt)-new Date(N.lastAccessedAt)),j=$.totalSize,V=Object.keys($.entries).length,Y=W.filter((J)=>{let N=j>X,H=V>Z;if(!N&&!H)return!1;return j-=(J.stateFileSize||0)+(J.promptStateSize||0),V-=1,!0});return await Promise.all(Y.map(async(J)=>{if(await o(J.stateFilePath).catch(()=>{}),J.promptStatePath)await o(J.promptStatePath).catch(()=>{});delete $.entries[J.id],console.log(`[SessionCache] Evicted entry: ${J.id}`)})),$.totalSize=Math.max(0,j),Y.map((J)=>J.id)},E6=async($,X,Z,W)=>{let j=Object.entries($.entries).filter(([V,Y])=>{if(V===Z)return!1;if(!m4(Y.metadata,W))return!1;return X.startsWith(Y.fullText)&&Y.fullText.length<X.length}).map(([,V])=>V);return await Promise.all(j.map(async(V)=>{if(await o(V.stateFilePath).catch(()=>{}),V.promptStatePath)await o(V.promptStatePath).catch(()=>{});$.totalSize-=(V.stateFileSize||0)+(V.promptStateSize||0),delete $.entries[V.id],console.log(`[SessionCache] Evicted superseded prefix entry: ${V.id} (${V.promptText.length} prompt chars)`)})),j.map((V)=>V.id)},M6=async($=n)=>{let X=o0($);try{let Z=await J6(X),W=Date.now(),j=3600000;await Promise.all(Z.map(async(V)=>{let Y=y.join(X,V),J=await B0(Y).catch(()=>null);if(J&&W-J.mtimeMs>3600000)await o(Y).catch(()=>{}),console.log(`[SessionCache] Cleaned up temp file: ${V}`)}))}catch{}},n1=async($)=>{try{return await B0($),!0}catch{return!1}},x6=($,X)=>{if($==null)return X;if(typeof $==="number")return $;if(typeof $==="string"){let Z=U6.parse($);return Z!=null?Z:X}return X},r1=async($,X={})=>{if(typeof fetch!=="function")throw Error("Global fetch is not available in this runtime");let Z=await fetch($,X);if(!Z.ok){let W=await Z.text().catch(()=>"");throw Error(`Failed to fetch ${$}: ${Z.status} ${Z.statusText} ${W}`.trim())}return Z.json()},o1=async($,X={})=>{if(typeof fetch!=="function")throw Error("Global fetch is not available in this runtime");let Z=await fetch($,{...X,method:"HEAD"});if(!Z.ok)throw Error(`Failed to fetch headers for ${$}: ${Z.status} ${Z.statusText}`);return Z},G2=async($,X,Z=n)=>{let W=JSON.stringify({url:$,headers:X}),j=await V2(W,"range-metadata",Z);if(j)return j;let V=!/^https?:/i.test($),{metadata:Y}=await V6($,{fetch,additionalFetchHeaders:X,allowLocalFile:V});return await g4(W,"range-metadata",Y,Z),Y},H2=($,X)=>{if($.model.local_path)return y.resolve($.model.local_path);let Z=X.repoId.split("/"),W=y.join($.runtime.cache_dir,...Z,X.revision);return y.join(W,X.filename)},P0=async($,X)=>{try{let Z=await B0($);if(!X)return!0;return Z.size===X}catch(Z){return!1}},a0=async($,X,Z,W,j)=>{if(typeof fetch!=="function")throw Error("Global fetch is not available in this runtime");await H0(y.dirname(Z));let V=await fetch($,{headers:X});if(!V.ok||!V.body)throw Error(`Failed to download ${$}: ${V.status} ${V.statusText}`);let Y=await Q6(Z,"w"),J=Number(V.headers.get("content-length"))||W||0,N=0,H=0.05;try{await V.body.pipeTo(new K6({async write(O){if(await Y.write(O),N+=O.byteLength,typeof j==="function"&&J>0){let q=Math.min(1,N/J);while(q>=H)j(H),H+=0.05}},async close(){if(await Y.close(),typeof j==="function")j(1)},async abort(O){throw await Y.close().catch(()=>{}),await o(Z).catch(()=>{}),O}}))}catch(O){throw await Y.close().catch(()=>{}),await o(Z).catch(()=>{}),O}if(W){let O=await B0(Z);if(O.size!==W)throw await o(Z).catch(()=>{}),Error(`Downloaded file size mismatch, expected ${W} got ${O.size}`)}},c4=async($)=>{let X=$.model.repo_id||$.model.repository||$.model.model;if(!X)throw Error("`model.repo_id` is required in Buttress backend config");let Z=$.model.revision||"main",W=$.runtime.cache_dir,j=JSON.stringify({repoId:X,revision:Z,filename:$.model.filename,url:$.model.url,quantization:$.model.quantization,preferred_quantizations:$.model.preferred_quantizations}),V=await V2(j,"artifact-info",W);if(V)return V;let Y={...$.runtime.http_headers||{}};if($.runtime.huggingface_token)Y.Authorization=`Bearer ${$.runtime.huggingface_token}`;if($.model.url){let Q=await o1($.model.url,{headers:Y}),K=Number(Q.headers.get("content-length"))||null,B=$.model.filename||$.model.url.split("/").pop(),L={repoId:X,revision:Z,filename:B,url:$.model.url,size:K,headers:Y};return await g4(j,"artifact-info",L,W),L}let{filename:J}=$.model,N=$.model.quantization&&String($.model.quantization).toLowerCase(),H=await r1(`${$.model.api_base}/models/${X}?revision=${Z}&blobs=true`,{headers:Y}),q=(H?.siblings||H?.files||[]).map((Q)=>Q.rfilename||Q.path||Q.filename).filter((Q)=>typeof Q==="string"&&Q.endsWith(q6));if(q.length===0)throw Error(`No GGUF artifacts found in repo ${X}`);let _=$.model.preferred_quantizations.length>0?$.model.preferred_quantizations:Q2,G=()=>{let Q=_.find((K)=>{return q.find((L)=>L.toLowerCase().includes(K))});if(Q)return{filename:q.find((B)=>B.toLowerCase().includes(Q)),quantization:Q};return null};if(!J){let Q=G()||{filename:q[0],quantization:null},{filename:K,quantization:B}=Q;J=K,N=B||l1(J)}else if(!N)N=l1(J);let w=`${$.model.base_url.replace(/\/+$/,"")}/${X}/resolve/${Z}/${J}`,z=/-(\d{5})-of-(\d{5})\.gguf$/,A=J.match(z),R=null;if(A){let[,,Q]=A,K=await r1(`${$.model.api_base}/models/${X}?revision=${Z}&blobs=true`,{headers:Y}),B=K?.siblings||K?.files||[],L=Number(Q);R=0;for(let F=1;F<=L;F+=1){let E=String(F).padStart(5,"0"),M=J.replace(z,`-${E}-of-${Q}.gguf`),x=B.find((I)=>(I.rfilename||I.path||I.filename)===M),D=Number(x?.size);if(Number.isFinite(D)&&D>0)R+=D}}else{let Q=await o1(w,{headers:Y});R=Number(Q.headers.get("content-length"))||null}let U={repoId:X,revision:Z,filename:J,url:w,size:R,quantization:N,headers:Y,isSplit:Boolean(A),splitCount:A?Number(A[2]):0};return await g4(j,"artifact-info",U,W),U},O2=async($,{modelBytes:X=null,kvCacheBytes:Z=null}={})=>{let W=_6($),[j,...V]=W,Y=$.backend?.gpu_memory_fraction!=null?Math.min(1,Math.max(0,Number($.backend.gpu_memory_fraction))):S0.backend.gpu_memory_fraction||1,J=$.backend?.cpu_memory_fraction!=null?Math.min(1,Math.max(0,Number($.backend.cpu_memory_fraction))):t0,N=await _0({platform:process.platform,totalMemoryInBytes:T0.totalmem(),backend:"ggml-llm",variant:j||null,preferVariants:V,gpuMemoryFraction:Y,cpuMemoryFraction:J,dependencies:{getBackendDevicesInfo:$2,isLibVariantAvailable:X2},modelBytes:X,kvCacheBytes:Z}),H=(_)=>({..._,devices:Array.isArray(_.devices)?_.devices:[],ok:_.ok,hasGpu:Boolean(_.hasGpu),totalMemory:_.gpuTotalBytes||_.totalMemory||0,error:_.ok?null:Error(_.error||`Variant ${_.variant} not available on this platform`)});if(!N.ok||!N.selected){let _=(N.attempts||[]).map((G)=>`${G.variant}: ${G.error||"unknown error"}`).join("; ");throw Error(`Unable to initialize any backend variant (${W.join(", ")}). Errors: ${_}`)}let O=(N.attempts||[]).map(H);return{selected:H(N.selected),attempts:O}},S6=async($)=>{let X=await c4($),Z=await G2(X.url,X.headers,$.runtime.cache_dir),{arch:W,nCtxTrain:j,nLayer:V,nEmbd:Y,nHead:J,nHeadKv:N,nEmbdHeadK:H,nEmbdHeadV:O,quantVersion:q,fileType:_,attentionLayerCount:G,recurrentLayerCount:w,ssmDConv:z,ssmDState:A,ssmDInner:R,ssmNGroup:U,ssmDtRank:Q,rwkvHeadSize:K,rwkvTokenShiftCount:B}=d0(Z),L=Number.isFinite(Number(V))?Number(V):0,F=Number.isFinite(Number(Y))?Number(Y):0,E=Number.isFinite(Number(J))?Number(J):0,M=Number.isFinite(Number(N))?Number(N):E,x=E>0&&F>0?F/E:128,D=H!=null&&Number.isFinite(Number(H))?Number(H):x,I=O!=null&&Number.isFinite(Number(O))?Number(O):x,b=c0({arch:W,metadata:Z,nLayer:L}),u=b&&Number.isFinite(Number(b.kvLayers))?Number(b.kvLayers):L,r=Math.max(0,Math.floor(Number(u)||0)),k={use_mmap:$.model.use_mmap??$.runtime.use_mmap,use_mlock:$.model.use_mlock??$.runtime.use_mlock,n_threads:$.model.n_threads??$.runtime.n_threads,n_ctx:$.model.n_ctx??$.runtime.n_ctx,n_batch:$.model.n_batch??$.runtime.n_batch,n_ubatch:$.model.n_ubatch??$.runtime.n_ubatch,n_cpu_moe:$.model.n_cpu_moe??$.runtime.n_cpu_moe,n_parallel:$.model.n_parallel??$.runtime.n_parallel,cpu_mask:$.model.cpu_mask??$.runtime.cpu_mask,cpu_strict:$.model.cpu_strict??$.runtime.cpu_strict,devices:$.model.devices??$.runtime.devices,n_gpu_layers:$.model.n_gpu_layers??$.runtime.n_gpu_layers,flash_attn_type:$.model.flash_attn_type??$.runtime.flash_attn_type,cache_type_k:$.model.cache_type_k??$.runtime.cache_type_k,cache_type_v:$.model.cache_type_v??$.runtime.cache_type_v,kv_unified:$.model.kv_unified??$.runtime.kv_unified,swa_full:$.model.swa_full??$.runtime.swa_full,ctx_shift:$.model.ctx_shift??$.runtime.ctx_shift},d=k.n_ctx?Number(k.n_ctx):null,v=d||j||4096,S=[],C=[],h=!0;if(d&&j&&d>j){h=!1;let V0=`Requested context length (${d}) exceeds model training context (${j})`;S.push(V0),C.push(V0),v=j}if(d&&!j)S.push("Model metadata missing training context length, using requested value");let p={k:k.cache_type_k,v:k.cache_type_v},f=X.size>0?X.size:0,s=G0({layerCount:r,headKvCount:M,embdHeadKCount:D,embdHeadVCount:I,cacheTypes:p,swaConfig:b,kvUnified:k.kv_unified,nParallel:k.n_parallel,swaFull:k.swa_full,arch:W,attentionLayerCount:G}),W0=l0({nLayer:L,nEmbd:F,recurrentLayerCount:w,nSeqMax:k.n_parallel||4,ssmDConv:z,ssmDState:A,ssmDInner:R,ssmNGroup:U,ssmDtRank:Q,rwkvHeadSize:K,rwkvTokenShiftCount:B,arch:W}),A0=s(v),l=await O2($,{modelBytes:f,kvCacheBytes:A0+W0}),L4=l.selected.totalMemory||0,F0=L4*($.backend.gpu_memory_fraction||1),A4=$.backend.cpu_memory_fraction!=null?Math.min(1,Math.max(0,Number($.backend.cpu_memory_fraction))):t0,h0=Math.max(0,T0.totalmem()*A4),I0=l.selected.hasGpu?F0:h0,U0=i0({maxCtx:v,availableMemory:I0,modelBytes:f,kvBytesForCtx:s});if(!d&&U0){let V0=j?Math.min(U0,j):U0,R4=Math.max(32,V0);if(R4<v)S.push(`Context length capped to ${R4} by memory limits`);v=R4}if(v>U0)v=U0;let C0=Math.floor(U0);console.log(`[buttress] Memory-limited context length: ${C0}`);let A1=s(v),p3=f+A1+W0,z1=L?f/(L+1):f,y0=0;if(l.selected.hasGpu&&z1>0)y0=Math.min(L+1,Math.max(0,Math.floor(F0/z1)));console.log(`[buttress] Auto GPU layer capacity (${l.selected.variant}): ${y0}/${L+1}`);let z4;if(k.n_gpu_layers==="auto"||k.n_gpu_layers==null)z4=y0;else z4=Math.max(0,Math.min(Number(k.n_gpu_layers)||0,L+1));let m3=(()=>{let V0=k.flash_attn_type&&String(k.flash_attn_type).toLowerCase();if(V0==="on"||V0==="off")return V0;if(V0==="auto")return l.selected.hasGpu?"auto":"off";return l.selected.hasGpu?"auto":"off"})(),c3=$.runtime.cache_dir,B4=H2($,X),B1=await P0(B4,X.size),d3={ok:h,backend:"ggml-llm",warnings:S,errors:C,model:{repoId:X.repoId,revision:X.revision,filename:X.filename,quantization:X.quantization,url:X.url,sizeBytes:X.size,metadata:{architecture:W,n_ctx_train:j,n_layer:L,n_embd:F,quantization_version:q,file_type:_,kv_layer_count:r,swa:b?.enabled?{window:b.window,pattern:b.pattern,dense_first:b.denseFirst,type:b.type,layers:b.swaLayers}:null}},runtime:{...k,variant:l.selected.variant,n_ctx:v,requested_ctx:d,n_gpu_layers:z4,auto_gpu_layers:y0,flash_attn_type:m3,cache_type_k:p.k,cache_type_v:p.v,estimated_max_n_ctx:C0},resources:{modelBytes:f,kvCacheBytes:A1,recurrentMemoryBytes:W0,totalEstimatedBytes:p3,gpuCapacityBytes:L4,gpuUsableBytes:F0,cpuUsableBytes:h0,fit:l.selected.fit},devices:{selected:l.selected,attempts:l.attempts},download:{cacheDir:c3,localPath:B4,exists:B1},timestamp:new Date().toISOString()};return{config:$,info:d3,artifact:X,metadata:{arch:W,nCtxTrain:j,nLayer:L,nEmbd:F},devices:l,cacheTypes:p,localPath:B4,localExists:B1}},P6=($,X,Z=null,W=null)=>{let j,V=Date.now(),Y=0;return new Z2({async start(J){try{let N=await $.parallel.completion(X,(G,w)=>{if(!w)return;if(w.token)Y+=1;J.enqueue({event:"token",data:{requestId:G,...w}})}),{requestId:H}=N;j=N.stop;let O=await N.promise;console.log("[Completion] Result:",O),J.enqueue({event:"result",data:{requestId:H,...O}}),J.close();let q=Date.now()-V,_=O.timings||{};$0.addCompletion({id:`completion-${H}`,generatorId:Z,requestId:H,repoId:W?.repoId||null,quantization:W?.quantization||null,variant:W?.variant||null,cacheTokens:_.cache_n??0,promptTokens:_.prompt_n??0,tokensGenerated:_.predicted_n??Y,tokensPerSecond:_.predicted_per_second??0,promptPerSecond:_.prompt_per_second??0,durationMs:q,success:!0,interrupted:O.interrupted||!1,contextFull:O.context_full||O.contextFull||!1})}catch(N){J.enqueue({event:"error",data:{message:N?.message||String(N)}}),J.error(N),$0.addCompletion({id:`completion-${Date.now()}`,generatorId:Z,repoId:W?.repoId||null,quantization:W?.quantization||null,variant:W?.variant||null,durationMs:Date.now()-V,tokensGenerated:Y,success:!1,error:N?.message||String(N)})}},cancel(){if(j)j()}})},T6=($,X,Z,W,j,V,Y=null,J=null,N=null)=>{let H,O="",q=!1,_=Date.now(),G=0,w=()=>{if(j)o(j).catch(()=>{});if(N)o(N).catch(()=>{})};return new Z2({async start(z){try{let A=await $.parallel.completion(X,(B,L)=>{if(!L)return;if(L.token)O+=L.token,G+=1;z.enqueue({event:"token",data:{requestId:B,...L}})}),{requestId:R}=A;H=A.stop;let U=await A.promise;if(U.text)O=U.text;else if(U.content)O=U.content;q=!U.interrupted&&!U.context_full,console.log("[Completion] Result:",U),z.enqueue({event:"result",data:{requestId:R,...U}}),z.close();let Q=Date.now()-_,K=U.timings||{};if($0.addCompletion({id:`completion-${R}`,generatorId:Y,requestId:R,repoId:J?.repoId||null,quantization:J?.quantization||null,variant:J?.variant||null,cacheTokens:K.cache_n??0,promptTokens:K.prompt_n??V??0,tokensGenerated:K.predicted_n??G,tokensPerSecond:K.predicted_per_second??0,promptPerSecond:K.prompt_per_second??0,durationMs:Q,success:!0,interrupted:U.interrupted||!1,contextFull:U.context_full||U.contextFull||!1,usedCache:Boolean(X.load_state_path)}),q&&Z.enabled&&O)Z.saveCompletionState(W,O,j,V,N).catch((B)=>{console.warn("[SessionCache] Save failed:",B.message)});else w()}catch(A){z.enqueue({event:"error",data:{message:A?.message||String(A)}}),z.error(A),$0.addCompletion({id:`completion-${Date.now()}`,generatorId:Y,repoId:J?.repoId||null,quantization:J?.quantization||null,variant:J?.variant||null,durationMs:Date.now()-_,tokensGenerated:G,success:!1,error:A?.message||String(A)}),w()}},cancel(){if(H)H();w()}})},z0=($)=>{let X={model:$.plan.localPath,runtime:$.plan.info.runtime};return p4("sha256").update(JSON.stringify(X)).digest("hex").slice(0,24)},k6=async($,X,Z,W=null)=>{let{config:j,localPath:V,artifact:Y}=$;if($.localExists&&!X.has(V)){if($.info.download.exists=!0,typeof Z==="function")Z(0.5);return V}if(j.model.local_path&&!j.model.allow_local_file)throw Error("Local model path provided but `model.allow_local_file` is not enabled");let J=V;if(W){let N=W.getDownload(J);if(N){console.log(`[ensureModelFile] Waiting for global download: ${Y.repoId}`);try{if(await N,await P0(V,Y.size)){if($.localExists=!0,$.info.download.exists=!0,typeof Z==="function")Z(0.5);return V}}catch(H){console.warn(`[ensureModelFile] Global download failed, will retry: ${H.message}`)}}}if(!X.has(J))X.set(J,(async()=>{if(Y.isSplit&&Y.splitCount>0){let N=/-(\d{5})-of-(\d{5})\.gguf$/,H=y.dirname(V),O=Y.splitCount,q=0;for(let _=1;_<=O;_+=1){let G=String(_).padStart(5,"0"),w=Y.filename.replace(N,`-${G}-of-${String(O).padStart(5,"0")}.gguf`),z=`${j.model.base_url.replace(/\/+$/,"")}/${Y.repoId}/resolve/${Y.revision}/${w}`,A=y.join(H,w);if(!await P0(A))await a0(z,Y.headers,A,null,(U)=>{if(U>=0&&Number.isFinite(U)){let Q=(q+U)/O,K=Math.round(Q*100);if(console.log(`Downloading model splits: ${Math.min(100,K)}%`),typeof Z==="function")Z(Q*0.5)}});q+=1}}else console.log("Downloading model: 0%"),await a0(Y.url,Y.headers,V,Y.size,(N)=>{if(N>=0&&Number.isFinite(N)){let H=Math.round(N*100);if(console.log(`Downloading model: ${Math.min(100,H)}%`),typeof Z==="function")Z(N*0.5)}});$.localExists=!0,$.info.download.exists=!0})());try{await X.get(J)}finally{X.delete(J)}return V},D6=async($,X)=>{let Z=z0($),W=$.contexts.get(Z);if(W&&!W.released){if(W.releaseTimer)clearTimeout(W.releaseTimer),W.releaseTimer=null,console.log(`[Context] Cancelled pending release for context "${Z}"`);if(W.releaseRequested=!1,W.refCount+=1,console.log(`[Context] Reusing existing context "${Z}", refCount=${W.refCount}`),typeof X==="function")X(0);if(!W.context)await W.ready;if(typeof X==="function")X(1);return W}if(W)console.log(`[Context] Record exists but released=${W.released}, creating new context`);else console.log(`[Context] No existing record for "${Z}", creating new context`);W={key:Z,refCount:1,ready:null,released:!1},$.contexts.set(Z,W),W.ready=(async()=>{let j=Date.now(),V=await k6($.plan,$.downloads,X,$.globalDownloadManager);if(typeof X==="function")X(0.5);let Y={model:V,n_threads:$.plan.info.runtime.n_threads,use_mmap:$.plan.info.runtime.use_mmap,use_mlock:$.plan.info.runtime.use_mlock,cpu_mask:$.plan.info.runtime.cpu_mask,cpu_strict:$.plan.info.runtime.cpu_strict,devices:$.plan.info.runtime.devices,n_ctx:$.plan.info.runtime.n_ctx,n_gpu_layers:$.plan.info.runtime.n_gpu_layers,n_parallel:$.plan.info.runtime.n_parallel,n_batch:$.plan.info.runtime.n_batch,n_ubatch:$.plan.info.runtime.n_ubatch,n_cpu_moe:$.plan.info.runtime.n_cpu_moe,flash_attn_type:$.plan.info.runtime.flash_attn_type,ctx_shift:$.plan.info.runtime.ctx_shift,kv_unified:$.plan.info.runtime.kv_unified,swa_full:$.plan.info.runtime.swa_full,lib_variant:$.plan.info.runtime.variant};if($.plan.info.runtime.flash_attn_type!=="off")Y.cache_type_k=$.plan.info.runtime.cache_type_k,Y.cache_type_v=$.plan.info.runtime.cache_type_v;console.log("[Context] Load Options:",Y);let J;try{if(J=await N6(Y,(N)=>{if(typeof X==="function"){if(X(0.5+N*0.25),N%5===0)console.log("[Context] Load Model Progress:",N)}}),$.plan.info.runtime.n_parallel){if(!await J.parallel.enable({n_parallel:$.plan.info.runtime.n_parallel,n_batch:$.plan.info.runtime.n_batch}))throw Error("Failed to enable parallel decoding mode for context")}if(typeof X==="function")X(1);return W.context=J,W.modelInfo=J.getModelInfo(),$0.addModelLoad({id:`${$.id}-${Z}`,generatorId:$.id,contextKey:Z,repoId:$.plan.info.model?.repoId||null,quantization:$.plan.info.model?.quantization||null,variant:$.plan.info.runtime?.variant||null,nCtx:$.plan.info.runtime?.n_ctx||null,nGpuLayers:$.plan.info.runtime?.n_gpu_layers||null,durationMs:Date.now()-j,success:!0}),W}catch(N){if($0.addModelLoad({id:`${$.id}-${Z}`,generatorId:$.id,contextKey:Z,repoId:$.plan.info.model?.repoId||null,quantization:$.plan.info.model?.quantization||null,variant:$.plan.info.runtime?.variant||null,durationMs:Date.now()-j,success:!1,error:N?.message||String(N)}),J)try{J.release()}catch(H){}throw N}})();try{return await W.ready,W}catch(j){throw $.contexts.delete(Z),j}},r0=async($,X,Z=!1)=>{if(X.released)return!1;if(!Z&&X.refCount>0)return!1;X.released=!0,$.contexts.delete(X.key);try{X.context?.parallel?.disable?.()}catch(W){}return await X.context?.release?.(),!0},b6=async($,X,Z=!1)=>{if(X.releaseRequested=!0,X.releaseTimer)clearTimeout(X.releaseTimer),X.releaseTimer=null;if(Z)X.refCount=0;else if(X.refCount=Math.max(0,X.refCount-1),X.refCount>0)return X.releaseRequested=!1,!1;let W=$.config.runtime.context_release_delay_ms;if(typeof W!=="number"||!Number.isFinite(W))return r0($,X);let j=Math.max(0,Math.floor(W));if(Z||j<=0)return r0($,X);return console.log(`[Context] Scheduling release in ${j}ms for context "${X.key}"`),X.releaseTimer=setTimeout(async()=>{if(X.releaseTimer=null,X.refCount>0){console.log(`[Context] Release cancelled, refCount=${X.refCount} for context "${X.key}"`),X.releaseRequested=!1;return}console.log(`[Context] Releasing context "${X.key}" after ${j}ms delay`),await r0($,X)},j),!0},d4=($)=>{let X=e0($);return X.model.repo_id||X.model.repository||X.model.model||null},a1=($)=>{if(!$)return 0;if(typeof $.score==="number"&&Number.isFinite($.score))return Number($.score);return q0($)};var l4=P(()=>{M0();s0();O6=H6(),{ReadableStream:Z2,WritableStream:K6}=O6,n=y.join(T0.homedir(),".buttress","models"),Q2=["mxfp4","q8_0","q6_k","q6","q5_k_m","q5_k_s","q5_k","q5_1","q5_0","q4_k_m","q4_k_s","q4_k","q4_1","q4_0","q3","q2"],S0={backend:{type:"ggml-llm",variant:null,variant_preference:["cuda","vulkan","snapdragon","default"],gpu_memory_fraction:0.85,cpu_memory_fraction:t0},model:{repo_id:null,revision:"main",filename:null,url:null,quantization:null,preferred_quantizations:[],n_ctx:null,n_gpu_layers:"auto",allow_local_file:!1,local_path:null,api_base:Y2,base_url:j2},runtime:{cache_dir:n,prefer_variants:[],huggingface_token:process.env.HUGGINGFACE_TOKEN||null,http_headers:{},session_cache:{enabled:!0,max_size_bytes:10737418240,max_entries:1000},context_release_delay_ms:1e4}}});import j0 from"node:path";import t4 from"node:os";import{stat as B2,mkdir as h6,open as I6,unlink as i4,readFile as C6,writeFile as y6}from"node:fs/promises";import{createHash as u6}from"node:crypto";import{initWhisper as f6}from"@fugood/whisper.node";import{getBackendDevicesInfo as R2,isLibVariantAvailable as w2}from"@fugood/llama.node";import*as g6 from"node:stream/web";class v2{constructor(){this.queue=[],this.processing=!1,this.currentTaskId=null}async enqueue($,X=null){return new Promise((Z,W)=>{this.queue.push({task:$,resolve:Z,reject:W,taskId:X}),this.processNext()})}async processNext(){if(this.processing||this.queue.length===0)return;this.processing=!0;let{task:$,resolve:X,reject:Z,taskId:W}=this.queue.shift();this.currentTaskId=W;try{let j=await $();X(j)}catch(j){Z(j)}finally{this.processing=!1,this.currentTaskId=null,this.processNext()}}getStatus(){return{processing:this.processing,queuedCount:this.queue.length,currentTaskId:this.currentTaskId}}}async function h2($,X,Z={}){let{globalDownloadManager:W=null}=Z,j=j4(X),V=await o6(j),Y={id:$,type:"ggml-stt",config:j,plan:V,info:V.info,contextRecord:null,downloads:new Map,globalDownloadManager:W,queue:new v2,finalized:!1},J=async()=>{if(Y.finalized)return;Y.finalized=!0;let A=Y.contextRecord;if(!A)return;if(A.released)return;if(A.releaseRequested||A.releaseTimer)return;if(A.refCount=Math.max(0,A.refCount-1),A.refCount>0)return;await Z4(Y,A)},N=async(A={})=>{let{onProgress:R}=A;try{let U=await e6(Y,R);return{modelInfo:U.modelInfo&&typeof U.modelInfo==="object"?{...U.modelInfo}:null,runtime:{...Y.plan.info.runtime},download:{...Y.plan.info.download}}}catch(U){throw console.error("[Context] Error initializing context:",U),U}},H=async()=>{if(Y.finalized)return!1;let A=Y.contextRecord;if(!A)return!1;return $9(Y,A)},O=async(A={})=>{let{audioPath:R,audioData:U,options:Q={}}=A,K=Y.contextRecord;if(!K)throw Error("Context not initialized");let B={...Q};if(Y.plan.info.runtime.max_threads&&B.maxThreads==null)B.maxThreads=Y.plan.info.runtime.max_threads;let L=`transcription-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,F=Date.now();return Y.queue.enqueue(async()=>{await K.ready;try{let E;if(U){let M=t6(U),{promise:x}=K.context.transcribeData(M,B);E=await x}else{if(!R)throw Error("audioPath or audioData is required for transcription");let M=j0.resolve(R),{promise:x}=K.context.transcribe(M,B);E=await x}return N0.addTranscription({id:L,generatorId:Y.id,repoId:Y.plan.info.model?.repoId||null,quantization:Y.plan.info.model?.quantization||null,modelType:Y.plan.info.model?.modelType||null,variant:Y.plan.info.runtime?.variant||null,durationMs:Date.now()-F,segmentCount:E?.segments?.length||0,textLength:E?.text?.length||0,success:!0}),E}catch(E){throw N0.addTranscription({id:L,generatorId:Y.id,repoId:Y.plan.info.model?.repoId||null,quantization:Y.plan.info.model?.quantization||null,modelType:Y.plan.info.model?.modelType||null,variant:Y.plan.info.runtime?.variant||null,durationMs:Date.now()-F,success:!1,error:E?.message||String(E)}),E}},L)},q=async(A={})=>O(A),_=async(A={})=>O(A),G=()=>{let A=Y.contextRecord;if(!A)return!1;return!A.released&&(A.releaseRequested||A.releaseTimer||A.refCount>0)},w=()=>{Y.finalized=!1},z=()=>({id:Y.id,type:Y.type,repoId:Y.plan.info.model?.repoId||null,quantization:Y.plan.info.model?.quantization||null,modelType:Y.plan.info.model?.modelType||null,variant:Y.plan.info.runtime?.variant||null,hasContext:Boolean(Y.contextRecord?.context),contextRefCount:Y.contextRecord?.refCount||0,queueStatus:Y.queue.getStatus()});return{id:$,type:"ggml-stt",info:V.info,queue:Y.queue,initContext:N,transcribe:q,transcribeData:_,releaseContext:H,finalize:J,getStatus:z,hasPendingReleases:G,resetFinalized:w}}async function I2($,X,Z={}){let{onProgress:W,onComplete:j,onError:V}=Z;try{let Y=j4($),J=await e4(Y),N=D2(Y,J),{repoId:H}=J;if(await W4(N,J.size)){if(console.log(`[Download] STT model already exists: ${H} at ${N}`),typeof j==="function")j({localPath:N,repoId:H,alreadyExists:!0});return{started:!1,localPath:N,repoId:H,alreadyExists:!0}}let q=X.getDownload(N);if(q)return console.log(`[Download] Already downloading STT model: ${H}`),q.then(()=>{if(typeof j==="function")j({localPath:N,repoId:H,joinedExisting:!0})}).catch((G)=>{if(typeof V==="function")V(G)}),{started:!1,localPath:N,repoId:H,alreadyDownloading:!0};console.log(`[Download] Starting STT model download: ${H}`);let _=(async()=>{try{if(await b2(J.url,J.headers,N,J.size,(G)=>{if(G>=0&&Number.isFinite(G)){if(console.log(`[Download] ${H}: ${Math.round(G*100)}%`),typeof W==="function")W(G)}}),console.log(`[Download] Completed STT model: ${H}`),typeof j==="function")j({localPath:N,repoId:H})}catch(G){if(console.error(`[Download] Failed STT model: ${H}`,G.message),typeof V==="function")V(G);throw G}finally{X.deleteDownload(N)}})();return X.setDownload(N,_),{started:!0,localPath:N,repoId:H}}catch(Y){if(console.error("[Download] Failed to start STT download:",Y.message),typeof V==="function")V(Y);return{started:!1,localPath:null,repoId:null,error:Y.message}}}async function C2($=null,X={}){let{threshold:Z=1.1,includeBreakdown:W=!1,config:j,...V}=X,Y=null,J=null,N=null;if(j)try{let R=j4(j),U=await e4(R);Y=U.size??null,{processingBufferBytes:J}=m0({modelBytes:Y}),N=U.quantization||null}catch(R){}let H=j?.backend?.gpu_memory_fraction!=null?Math.min(1,Math.max(0,Number(j.backend.gpu_memory_fraction))):void 0,O=j?.backend?.cpu_memory_fraction!=null?Math.min(1,Math.max(0,Number(j.backend.cpu_memory_fraction))):void 0,q=await _0({...V,platform:process.platform,totalMemoryInBytes:t4.totalmem(),backend:"ggml-stt",includeBreakdown:W,gpuMemoryFraction:H,cpuMemoryFraction:O,dependencies:{getBackendDevicesInfo:R2,isLibVariantAvailable:w2},modelBytes:Y,kvCacheBytes:J}),_=q.selected,G=z2(_);if(_)_.modelBytes=Y||null,_.processingBytes=J||null,_.quantization=N||null;let w=null,z=null;if($){let R=z2($);z={...$,score:R};let U="buttress",Q="buttress-higher-score";if(!q.ok)U="local",Q="buttress-unavailable";else if(!R&&R!==0)U="buttress",Q="missing-client-score";else if($.fit&&_?.fit){let K=$.fit.fitsInGpu||$.fit.fitsInCpu,B=_.fit.fitsInGpu||_.fit.fitsInCpu;if(K&&!B)U="local",Q="client-fits-in-memory";else if(B&&!K)U="buttress",Q="buttress-fits-in-memory";else if(R>G*Z)U="local",Q="client-better";else if(G>R*Z)U="buttress",Q="buttress-better";else U="either",Q="comparable-scores"}else if(R>G*Z)U="local",Q="client-better";else if(G>R*Z)U="buttress",Q="buttress-better";else U="either",Q="comparable-scores";w={buttressScore:G,clientScore:R,threshold:Z,recommendation:U,reason:Q}}if(!q.ok&&!w)w={buttressScore:G,clientScore:$?.score??null,threshold:Z,recommendation:"local",reason:"buttress-unavailable"};let A=null;if(j)A={repoId:j.model?.repo_id||null,quantization:j.model?.quantization||null,filename:j.model?.filename||null};return{type:"ggml-stt",timestamp:new Date().toISOString(),buttress:q,client:z,comparison:w,modelConfig:A}}var p6=()=>{if(typeof globalThis<"u"&&globalThis.ReadableStream&&globalThis.WritableStream)return{ReadableStream:globalThis.ReadableStream,WritableStream:globalThis.WritableStream};return g6},m6,F2=($={},X={})=>{return Object.entries(X||{}).forEach(([Z,W])=>{if(W&&typeof W==="object"&&!Array.isArray(W)){if(!$[Z]||typeof $[Z]!=="object")$[Z]={};F2($[Z],W)}else $[Z]=W}),$},c6=".bin",E2="https://huggingface.co",M2="https://huggingface.co/api",R0,r4,o4,x2="fp16",S2=0.5,d6,P2=($)=>{if(!$)return null;let X=$.toLowerCase();return d6.find((Z)=>X.includes(Z))||null},a4,s4=($,X=[])=>{if(!$&&$!==0)return[...X];if(Array.isArray($))return $.filter((Z)=>Z!=null);return[$]},X4=($)=>{if(!$)return null;let X=String($).toLowerCase();if(["cuda","vulkan","default"].includes(X))return X;return null},j4=($={})=>{let X=JSON.parse(JSON.stringify(a4));if(F2(X,$),X.backend.variant=X4(X.backend.variant),X.backend.variant_preference=Array.from(new Set(s4(X.backend.variant_preference||r4).map(X4).filter(Boolean))),X.backend.variant_preference.length===0)X.backend.variant_preference=[...r4];if(X.runtime.prefer_variants=Array.from(new Set(s4(X.runtime.prefer_variants).map(X4).filter(Boolean))),X.model.preferred_quantizations=Array.from(new Set(s4(X.model.preferred_quantizations||X.model.quantizations).map((Z)=>Z?String(Z).toLowerCase():null).filter(Boolean))),X.model.quantization){let Z=String(X.model.quantization).toLowerCase();if(!X.model.preferred_quantizations.includes(Z))X.model.preferred_quantizations.unshift(Z)}return X.model.base_url=X.model.base_url||E2,X.model.api_base=X.model.api_base||M2,X.runtime.cache_dir=X.runtime.cache_dir?j0.resolve(X.runtime.cache_dir):R0,X.runtime.context_release_delay_ms=Math.max(0,Number(X.runtime.context_release_delay_ms)||a4.runtime.context_release_delay_ms),X},n4=($)=>{let X=$.toLowerCase();return o4.find((W)=>X.includes(W))||null},l6=($)=>{let X=[];if($.backend.variant)X.push($.backend.variant);if($.runtime.prefer_variants.length>0)X.push(...$.runtime.prefer_variants);return X.push(...$.backend.variant_preference),X.push("default"),Array.from(new Set(X.map(X4).filter(Boolean)))},T2=async($)=>{await h6($,{recursive:!0})},i6=($=R0)=>j0.join($,".metadata-cache"),k2=($,X,Z=R0)=>{let W=u6("sha256").update($).digest("hex");return j0.join(i6(Z),X,`${W}.json`)},s6=async($,X,Z=R0)=>{try{let W=k2($,X,Z),j=await C6(W,"utf-8");return JSON.parse(j)}catch(W){return null}},L2=async($,X,Z,W=R0)=>{try{let j=k2($,X,W);await T2(j0.dirname(j)),await y6(j,JSON.stringify(Z),"utf-8")}catch(j){}},n6=async($,X={})=>{if(typeof fetch!=="function")throw Error("Global fetch is not available in this runtime");let Z=await fetch($,X);if(!Z.ok){let W=await Z.text().catch(()=>"");throw Error(`Failed to fetch ${$}: ${Z.status} ${Z.statusText} ${W}`.trim())}return Z.json()},A2=async($,X={})=>{if(typeof fetch!=="function")throw Error("Global fetch is not available in this runtime");let Z=await fetch($,{...X,method:"HEAD"});if(!Z.ok)throw Error(`Failed to fetch headers for ${$}: ${Z.status} ${Z.statusText}`);return Z},D2=($,X)=>{if($.model.local_path)return j0.resolve($.model.local_path);let Z=X.repoId.split("/"),W=j0.join($.runtime.cache_dir,...Z,X.revision);return j0.join(W,X.filename)},W4=async($,X)=>{try{let Z=await B2($);if(!X)return!0;return Z.size===X}catch(Z){return!1}},b2=async($,X,Z,W,j)=>{if(typeof fetch!=="function")throw Error("Global fetch is not available in this runtime");await T2(j0.dirname(Z));let V=await fetch($,{headers:X});if(!V.ok||!V.body)throw Error(`Failed to download ${$}: ${V.status} ${V.statusText}`);let Y=await I6(Z,"w"),J=Number(V.headers.get("content-length"))||W||0,N=0,H=0.05;try{await V.body.pipeTo(new m6({async write(O){if(await Y.write(O),N+=O.byteLength,typeof j==="function"&&J>0){let q=Math.min(1,N/J);while(q>=H)j(H),H+=0.05}},async close(){if(await Y.close(),typeof j==="function")j(1)},async abort(O){throw await Y.close().catch(()=>{}),await i4(Z).catch(()=>{}),O}}))}catch(O){throw await Y.close().catch(()=>{}),await i4(Z).catch(()=>{}),O}if(W){let O=await B2(Z);if(O.size!==W)throw await i4(Z).catch(()=>{}),Error(`Downloaded file size mismatch, expected ${W} got ${O.size}`)}},e4=async($)=>{let X=$.model.repo_id||$.model.repository||$.model.model;if(!X)throw Error("`model.repo_id` is required in Buttress backend config");let Z=$.model.revision||"main",W=$.runtime.cache_dir,j=JSON.stringify({repoId:X,revision:Z,filename:$.model.filename,url:$.model.url,quantization:$.model.quantization,preferred_quantizations:$.model.preferred_quantizations}),V=await s6(j,"artifact-info",W);if(V)return V;let Y={...$.runtime.http_headers||{}};if($.runtime.huggingface_token)Y.Authorization=`Bearer ${$.runtime.huggingface_token}`;if($.model.url){let U=await A2($.model.url,{headers:Y}),Q=Number(U.headers.get("content-length"))||null,K=$.model.filename||$.model.url.split("/").pop(),B={repoId:X,revision:Z,filename:K,url:$.model.url,size:Q,quantization:n4(K||""),headers:Y};return await L2(j,"artifact-info",B,W),B}let{filename:J}=$.model,N=$.model.quantization&&String($.model.quantization).toLowerCase(),H=await n6(`${$.model.api_base}/models/${X}?revision=${Z}&blobs=true`,{headers:Y}),q=(H?.siblings||H?.files||[]).map((U)=>U.rfilename||U.path||U.filename).filter((U)=>typeof U==="string"&&U.endsWith(c6));if(q.length===0)throw Error(`No model artifacts found in repo ${X}`);let _=$.model.preferred_quantizations.length>0?$.model.preferred_quantizations:o4,G=()=>{for(let U of _)if(U===x2){let Q=q.find((K)=>{let B=K.toLowerCase();return!o4.some((L)=>B.includes(L))});if(Q)return{filename:Q,quantization:null}}else{let Q=q.find((K)=>K.toLowerCase().includes(U));if(Q)return{filename:Q,quantization:U}}return null};if(!J){let U=G()||{filename:q[0],quantization:null},{filename:Q,quantization:K}=U;J=Q,N=K||n4(J)}else if(!N)N=n4(J);let w=`${$.model.base_url.replace(/\/+$/,"")}/${X}/resolve/${Z}/${J}`,z=await A2(w,{headers:Y}),A=Number(z.headers.get("content-length"))||null,R={repoId:X,revision:Z,filename:J,url:w,size:A,quantization:N,headers:Y,isSplit:!1,splitCount:0};return await L2(j,"artifact-info",R,W),R},r6=async($,{modelBytes:X=null,processingBytes:Z=null}={})=>{let W=l6($),[j,...V]=W,Y=$.backend?.gpu_memory_fraction!=null?Math.min(1,Math.max(0,Number($.backend.gpu_memory_fraction))):a4.backend.gpu_memory_fraction||1,J=$.backend?.cpu_memory_fraction!=null?Math.min(1,Math.max(0,Number($.backend.cpu_memory_fraction))):S2,N=await _0({platform:process.platform,totalMemoryInBytes:t4.totalmem(),backend:"ggml-stt",variant:j||null,preferVariants:V,variantPreference:$.backend.variant_preference,gpuMemoryFraction:Y,cpuMemoryFraction:J,dependencies:{getBackendDevicesInfo:R2,isLibVariantAvailable:w2},modelBytes:X,kvCacheBytes:Z}),H=(_)=>({..._,devices:Array.isArray(_.devices)?_.devices:[],ok:_.ok,hasGpu:Boolean(_.hasGpu),totalMemory:_.gpuTotalBytes||_.totalMemory||0,error:_.ok?null:Error(_.error||`Variant ${_.variant} not available on this platform`)});if(!N.ok||!N.selected){let _=(N.attempts||[]).map((G)=>`${G.variant}: ${G.error||"unknown error"}`).join("; ");throw Error(`Unable to initialize any backend variant (${W.join(", ")}). Errors: ${_}`)}let O=(N.attempts||[]).map(H);return{selected:H(N.selected),attempts:O}},o6=async($)=>{let X=await e4($),Z=m0({modelBytes:X.size>0?X.size:0}),W=await r6($,{modelBytes:Z.modelBytes,processingBytes:Z.processingBufferBytes}),j=W.selected.hasGpu&&(W.selected.fit?.fitsInGpu!==void 0?W.selected.fit.fitsInGpu:!0);if($.model.use_gpu===!1)j=!1;let V=$.model.use_flash_attn&&String($.model.use_flash_attn).toLowerCase(),Y;if(V==="on"||V==="true")Y=!0;else if(V==="off"||V==="false")Y=!1;else Y=j;let J=$.runtime.cache_dir,N=D2($,X),H=await W4(N,X.size),O={ok:!0,backend:"ggml-stt",model:{repoId:X.repoId,revision:X.revision,filename:X.filename,quantization:X.quantization,modelType:P2(X.filename),url:X.url,sizeBytes:X.size},runtime:{variant:W.selected.variant,use_gpu:j,use_flash_attn:Y,max_threads:$.runtime.max_threads?Number($.runtime.max_threads):null},resources:{...Z,gpuCapacityBytes:W.selected.gpuTotalBytes,gpuUsableBytes:W.selected.gpuUsableBytes,cpuUsableBytes:W.selected.cpuUsableBytes,fit:W.selected.fit},devices:{selected:W.selected,attempts:W.attempts},download:{cacheDir:J,localPath:N,exists:H},timestamp:new Date().toISOString()};return{config:$,info:O,artifact:X,memory:Z,devices:W,localPath:N,localExists:H}},a6=async($,X,Z,W=null)=>{let{localPath:j,artifact:V,config:Y}=$;if($.localExists){if(typeof Z==="function")Z(1);return j}if(W){let H=W.getDownload(j);if(H){console.log(`[ensureModelFile] Waiting for global STT download: ${V.repoId}`);try{if(await H,await W4(j,V.size)){if($.localExists=!0,$.info.download.exists=!0,typeof Z==="function")Z(1);return j}}catch(O){console.warn(`[ensureModelFile] Global STT download failed, will retry: ${O.message}`)}}}let J=X.get(j);if(J){if(await J,typeof Z==="function")Z(1);return j}let N=(async()=>{if(Y.model.allow_local_file){if(!await W4(j,V.size))throw Error(`Local model file not found: ${j}`);return j}return await b2(V.url,V.headers,j,V.size,Z),j})();X.set(j,N);try{return await N,j}finally{X.delete(j)}},t6=($)=>{if(!$)return null;if($ instanceof ArrayBuffer)return $;if(ArrayBuffer.isView($))return $.buffer;if(typeof $==="string"){let X=$.startsWith("data:")?$.split(",")[1]||"":$,Z=Buffer.from(X,"base64");return Z.buffer.slice(Z.byteOffset,Z.byteOffset+Z.byteLength)}throw Error("Unsupported audioData format, expected base64 string or ArrayBuffer")},e6=async($,X)=>{if($.contextRecord&&!$.contextRecord.released){if($.contextRecord.releaseTimer)clearTimeout($.contextRecord.releaseTimer),$.contextRecord.releaseTimer=null,console.log("[Context] Cancelled pending STT release");if($.contextRecord.releaseRequested=!1,$.contextRecord.refCount+=1,console.log(`[Context] Reusing existing STT context, refCount=${$.contextRecord.refCount}`),typeof X==="function")X(0);if(!$.contextRecord.context)await $.contextRecord.ready;if(typeof X==="function")X(1);return $.contextRecord}if($.contextRecord)console.log(`[Context] STT record exists but released=${$.contextRecord.released}, creating new context`);else console.log("[Context] No existing STT record, creating new context");let Z={refCount:1,ready:null,released:!1};$.contextRecord=Z,Z.ready=(async()=>{let W=Date.now();try{if(typeof X==="function")X(0);let j=await a6($.plan,$.downloads,X,$.globalDownloadManager);if(typeof X==="function")X(0.5);let V=await f6({filePath:j,useFlashAttn:$.plan.info.runtime.flash_attn_type==="on",useGpu:$.plan.info.runtime.n_gpu_layers>0,nThreads:$.plan.info.runtime.n_threads},$.plan.info.runtime.variant);if(typeof X==="function")X(1);Z.context=V;try{Z.modelInfo=V.getModelInfo()}catch(Y){Z.modelInfo=null}return N0.addModelLoad({id:$.id,generatorId:$.id,repoId:$.plan.info.model?.repoId||null,quantization:$.plan.info.model?.quantization||null,modelType:$.plan.info.model?.modelType||null,variant:$.plan.info.runtime?.variant||null,useGpu:$.plan.info.runtime?.use_gpu||!1,durationMs:Date.now()-W,success:!0}),Z}catch(j){throw N0.addModelLoad({id:$.id,generatorId:$.id,repoId:$.plan.info.model?.repoId||null,quantization:$.plan.info.model?.quantization||null,modelType:$.plan.info.model?.modelType||null,variant:$.plan.info.runtime?.variant||null,durationMs:Date.now()-W,success:!1,error:j?.message||String(j)}),j}})();try{if(await Z.ready,typeof X==="function")X(1);return Z}catch(W){throw $.contextRecord=null,W}},Z4=async($,X,Z=!1)=>{if(X.released)return!1;if(!Z&&X.refCount>0)return!1;return X.released=!0,$.contextRecord=null,await X.context?.release?.(),!0},$9=async($,X,Z=!1)=>{if(X.releaseRequested=!0,X.releaseTimer)clearTimeout(X.releaseTimer),X.releaseTimer=null;if(Z)X.refCount=0;else if(X.refCount=Math.max(0,X.refCount-1),X.refCount>0)return X.releaseRequested=!1,!1;let W=$.config.runtime.context_release_delay_ms;if(typeof W!=="number"||!Number.isFinite(W))return Z4($,X);let j=Math.max(0,Math.floor(W));if(Z||j<=0)return Z4($,X);return console.log(`[Context] Scheduling STT release in ${j}ms`),X.releaseTimer=setTimeout(async()=>{if(X.releaseTimer=null,X.refCount>0){console.log(`[Context] STT release cancelled, refCount=${X.refCount}`),X.releaseRequested=!1;return}console.log(`[Context] Releasing STT context after ${j}ms delay`),await Z4($,X)},j),!0},$1=($)=>{let X=j4($),Z=X.model.repo_id||X.model.repository||X.model.model||null;if(!Z)return null;let W=P2(X.model.filename);if(W)return`${Z}:${W}`;return Z},z2=($)=>{if(!$)return 0;if(typeof $.score==="number"&&Number.isFinite($.score))return Number($.score);return q0($)};var X1=P(()=>{M0();s0();({WritableStream:m6}=p6()),R0=j0.join(t4.homedir(),".buttress","models"),r4=["cuda","vulkan","default"],o4=["q8_0","q5_1","q5_0","q4_1","q4_0"],d6=["large-v3-turbo","distil-large-v3","large-v3","large-v2","large-v1","large","distil-medium","medium.en","medium","small.en-tdrz","distil-small.en","small.en","small","base.en","base","tiny.en","tiny"],a4={backend:{type:"ggml-stt",variant:null,variant_preference:r4,gpu_memory_fraction:0.85,cpu_memory_fraction:S2},model:{repo_id:"BricksDisplay/whisper-ggml",revision:"main",filename:null,url:null,quantization:null,preferred_quantizations:["q8_0",x2,"q5_1"],allow_local_file:!1,local_path:null,api_base:M2,base_url:E2,use_gpu:!0,use_flash_attn:"auto"},runtime:{cache_dir:R0,prefer_variants:[],huggingface_token:process.env.HUGGINGFACE_TOKEN||null,http_headers:{},max_threads:null,context_release_delay_ms:1e4}}});async function t($,X=null,Z={}){if($==="ggml-llm")return _2(X,Z);if($==="ggml-stt")return C2(X,Z);throw Error(`Unknown backend type: ${$}`)}var Z1=P(()=>{l4();X1()});var g;var y2=P(()=>{g={name:"@fugood/buttress-backend-core",private:!0,type:"module",version:"2.23.0-beta.53",main:"src/index.js",types:"lib/types/index.d.ts",scripts:{build:"tsc --noResolve --noCheck --declaration --emitDeclarationOnly --allowJs --outDir lib/types src/index.js"},dependencies:{"@fugood/buttress-hardware-guardrails":"^2.23.0-beta.53","@fugood/llama.node":"^1.6.0-rc.0","@fugood/whisper.node":"^1.0.13","@huggingface/gguf":"^0.3.2","@iarna/toml":"^3.0.0",bytes:"^3.1.0"}}});import m from"node:os";import u2 from"node:fs";import f2 from"node:path";import{execSync as Y4}from"node:child_process";import g2 from"@iarna/toml";async function m2({modelIds:$=[],defaultConfig:X=null}={}){let Z=[];if(console.log(`${g.name} v${g.version}`),console.log(`Generating model capabilities comparison...
|
|
3
|
+
`),Z.push(`${g.name} v${g.version}`),Z.push(`## Model Capabilities Comparison
|
|
4
|
+
`),!$||$.length===0)console.error("Error: No model IDs provided"),process.exit(1);try{let W=(U={},Q={})=>{let K=Array.isArray(U)?[...U]:{...U};return Object.entries(Q||{}).forEach(([B,L])=>{if(L&&typeof L==="object"&&!Array.isArray(L))K[B]=W(K[B]||{},L);else K[B]=L}),K},j=X||{},{server:V,generators:Y=[],...J}=j,N=(U)=>W(JSON.parse(JSON.stringify(J)),U||{}),H=(U)=>{if(Array.isArray(Y)&&Y.length>0){let Q=Y.filter((K)=>K?.type==="ggml-llm");if(Q.length>0&&U){let K=Q.find((B)=>B.model?.repo_id===U);if(K)return N(K)}}return Object.keys(J).length>0?N({}):null},O=[];for(let U=0;U<$.length;U+=1){let Q=$[U];console.log(`[${U+1}/${$.length}] Analyzing ${Q}...`);let K=H(Q);K={...K||{},model:{...J.runtime,...K?.model||{},repo_id:Q}};let B=await t("ggml-llm",null,{config:K,includeBreakdown:!0});O.push({modelId:Q,capabilities:B,modelInfo:B.buttress?.selected||null,modelConfig:B.modelConfig||null})}let q=(U)=>U?(U/1024/1024/1024).toFixed(2):"N/A",_=(U)=>U?"✅":"\uD83D\uDEAB";Z.push("| Model ID | Quantization | Size (GB) | Context Size | KV Cache Size (GB) | Total Required Memory (GB) | Fits GPU (Full) | Fits CPU (Full) |"),Z.push("|----------|--------------|-----------|--------------|--------------------|-----------------------------|-----------------|-----------------|"),O.forEach(({modelId:U,modelInfo:Q,modelConfig:K})=>{let B=Q?.quantization?.name?.toUpperCase()||"N/A",L=q(Q?.modelBytes),F=K?.nCtx||Q?.kvInfo?.nCtxTrain||"N/A",E=G0(Q),M=Number(F),x=Q?.kvCacheBytes||(E&&Number.isFinite(M)&&M>0?E(M):E&&E(Q?.kvInfo?.nCtxTrain||0))||null,D=q(x),I=q(Q?.modelBytes&&x?Q.modelBytes+x:Q?.fit?.totalRequiredBytes),b=_(Q?.fit?.fitsInGpu),u=_(Q?.fit?.fitsInCpu);Z.push(`| ${U} | ${B} | ${L} | ${F} | ${D} | ${I} | ${b} | ${u} |`);let r=Q?.memoryLimitedCtx!=null||Q?.limitedFit!=null,k=!Q?.fit?.fitsInGpu||!Q?.fit?.fitsInCpu;if(r&&k){let d=Q?.memoryLimitedCtx||F,v=Number(d),S=Q?.limitedKvCacheBytes||E&&Number.isFinite(v)&&v>0&&E(v)||null,C=q(S),h=q(Q?.modelBytes&&S?Q.modelBytes+S:Q?.limitedFit?.totalRequiredBytes),p=_(Q?.limitedFit?.fitsInGpu),f=_(Q?.limitedFit?.fitsInCpu);if(d!==F||C!==D||h!==I)Z.push(`| ↳ Limited | - | ${L} | ${d} | ${C} | ${h} | ${p} | ${f} |`)}}),Z.push(`
|
|
5
|
+
---`),Z.push(`
|
|
6
|
+
### System Information`);let G=null;if(process.platform!=="win32")try{G=Y4("uname -a",{encoding:"utf8"}).trim()}catch{}if(G)Z.push(`- **System:** ${G}`);else Z.push(`- **Hostname:** ${m.hostname()}`),Z.push(`- **OS:** ${m.type()} ${m.release()}`);if(Z.push(`- **Platform:** ${process.platform}`),Z.push(`- **CPU Cores:** ${m.cpus().length}`),Z.push(`- **Total System Memory:** ${(m.totalmem()/1024/1024/1024).toFixed(2)} GB`),O.length>0){let Q=O[0].capabilities.buttress?.selected;if(Q){let K=Q.cpuTotalBytes>0?(Q.cpuUsableBytes/Q.cpuTotalBytes*100).toFixed(0):0;if(Z.push(`- **Usable CPU Memory:** ${(Q.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${K}% of ${(Q.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),Q.hasGpu){let B=Q.devices.filter((L)=>L.type==="gpu");if(B.length>0){let L=B[0];Z.push(`- **GPU Backend:** ${L.backend}`),Z.push(`- **GPU Name:** ${L.deviceName}`),Z.push(`- **GPU Total Memory:** ${(L.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let F=Q.gpuTotalBytes>0?(Q.gpuUsableBytes/Q.gpuTotalBytes*100).toFixed(0):0;Z.push(`- **GPU Usable Memory:** ${(Q.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${F}% of ${(Q.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`)}}else Z.push("- **GPU:** Not available")}}Z.push(`
|
|
7
|
+
### Command Used`);let w=process.argv.slice(2).join(" ");if(Z.push(`\`\`\`bash
|
|
8
|
+
${process.argv[0]} ${process.argv[1]} ${w}
|
|
9
|
+
\`\`\``),Z.push(`
|
|
10
|
+
### Package Information`),Z.push(`- **Name:** ${g.name}`),Z.push(`- **Version:** ${g.version}`),g.description)Z.push(`- **Description:** ${g.description}`);if(X&&Object.keys(X).length>0){Z.push(`
|
|
11
|
+
### Configuration`),Z.push("<details>"),Z.push("<summary>Click to expand TOML configuration</summary>"),Z.push("\n```toml");try{let U=g2.stringify(X);Z.push(U)}catch(U){Z.push("# Error serializing config"),Z.push(JSON.stringify(X,null,2))}Z.push("```"),Z.push("</details>")}let A=`ggml-llm-model-capabilities-${new Date().toISOString().replace(/[.:]/g,"-").split("T")[0]}.md`,R=f2.join(process.cwd(),A);u2.writeFileSync(R,Z.join(`
|
|
12
|
+
`),"utf8"),console.log(`
|
|
13
|
+
Model capabilities table saved to: ${R}`),process.exit(0)}catch(W){console.error("Failed to generate model table:",W.message),process.exit(1)}}async function c2({modelId:$=null,defaultConfig:X=null}={}){if(console.log(`${g.name} v${g.version}`),console.log("Testing capabilities for backend: ggml-llm"),$)console.log(`Model: ${$}`);console.log("--------------------------------");try{let Z=X||{},{server:W,generators:j=[],...V}=Z,Y=(G={},w={})=>{let z=Array.isArray(G)?[...G]:{...G};return Object.entries(w||{}).forEach(([A,R])=>{if(R&&typeof R==="object"&&!Array.isArray(R))z[A]=Y(z[A]||{},R);else z[A]=R}),z},J=(G)=>Y(JSON.parse(JSON.stringify(V)),G||{}),H=((G)=>{if(Array.isArray(j)&&j.length>0){let w=j.filter((z)=>z?.type==="ggml-llm");if(w.length>0){if(G){let z=w.find((A)=>A.model?.repo_id===G);if(z)return J(z)}}}if(Object.keys(V).length>0)return J({});return null})($);if($)H={...H||{},model:{...H?.model||{},repo_id:$}};let O=await t("ggml-llm",null,{config:H,includeBreakdown:!0}),q=O.buttress?.selected||null,_=O.modelConfig||null;if($||_?.repoId){console.log(`
|
|
14
|
+
=== Model Information ===`);let G=$||_?.repoId;if(console.log(`Repository ID: ${G}`),_?.quantization)console.log(`Quantization: ${_.quantization}`);if(_?.nCtx)console.log(`Context Length: ${_.nCtx}`);if(q?.quantization?.name)console.log(`Model Quantization: ${q.quantization.name.toUpperCase()}`);let w=_?.cache_type_k||"f16",z=_?.cache_type_v||"f16";if(console.log(`KV Cache Type: K=${w}, V=${z}`),q?.modelBytes&&q?.kvCacheBytes){if(console.log(`Model Size: ${(q.modelBytes/1024/1024/1024).toFixed(2)} GB`),q.kvInfo)console.log(`KV Cache Size: ${(q.kvCacheBytes/1024/1024/1024).toFixed(2)} GB (KV info: ${JSON.stringify(q.kvInfo)})`);else console.log(`KV Cache Size: ${(q.kvCacheBytes/1024/1024/1024).toFixed(2)} GB`);if(console.log(`Total Required Memory: ${((q.modelBytes+q.kvCacheBytes)/1024/1024/1024).toFixed(2)} GB`),q.memoryLimitedCtx!=null){let A=q.memoryLimitedCtx,R=q.kvInfo?.nCtxTrain;if(R)console.log(`
|
|
15
|
+
Memory-Limited Context: ${A} (Train: ${R})`);else console.log(`
|
|
16
|
+
Memory-Limited Context: ${A}`);if(q.limitedKvCacheBytes!=null)console.log(`Limited KV Cache Size: ${(q.limitedKvCacheBytes/1024/1024/1024).toFixed(2)} GB`)}}else if(O.buttress?.selected?.fit){let{totalRequiredBytes:A}=O.buttress.selected.fit;console.log(`Total Required Memory: ${(A/1024/1024/1024).toFixed(2)} GB`)}}if(O.buttress?.selected){let{selected:G}=O.buttress;console.log(`
|
|
17
|
+
=== Hardware Information ===`);let w=null;if(process.platform!=="win32")try{w=Y4("uname -a",{encoding:"utf8"}).trim()}catch{}if(w)console.log(`System: ${w}`);else console.log(`Hostname: ${m.hostname()}`),console.log(`OS: ${m.type()} ${m.release()}`);console.log(`Platform: ${G.platform}`),console.log(`CPU Cores: ${m.cpus().length}`),console.log(`Total System Memory: ${(m.totalmem()/1024/1024/1024).toFixed(2)} GB`);let z=G.cpuTotalBytes>0?(G.cpuUsableBytes/G.cpuTotalBytes*100).toFixed(0):0;if(console.log(`Usable CPU Memory: ${(G.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${z}% of ${(G.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),G.hasGpu)console.log(`
|
|
18
|
+
--- GPU Details ---`),G.devices.filter((R)=>R.type==="gpu").forEach((R)=>{console.log(`GPU Backend: ${R.backend}`),console.log(`GPU Name: ${R.deviceName}`),console.log(`GPU Total Memory: ${(R.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let U=G.gpuTotalBytes>0?(G.gpuUsableBytes/G.gpuTotalBytes*100).toFixed(0):0;if(console.log(`GPU Usable Memory: ${(G.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${U}% of ${(G.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),R.metadata){if(R.metadata.hasBFloat16)console.log("Supports BFloat16: Yes");if(R.metadata.hasUnifiedMemory)console.log("Unified Memory: Yes")}});else console.log("GPU: Not available");if(console.log(`
|
|
19
|
+
Backend Variant: ${G.variant}`),console.log(`Performance Score: ${G.score}`),G.fit){if(console.log(`
|
|
20
|
+
--- Model Fit Analysis ---`),console.log(`Fits in GPU: ${G.fit.fitsInGpu?"Yes":"No"}`),console.log(`Fits in CPU: ${G.fit.fitsInCpu?"Yes":"No"}`),console.log(`Limiting Factor: ${G.fit.limiting}`),G.limitedFit)console.log(`
|
|
21
|
+
--- Memory-Limited Fit Analysis ---`),console.log(`Limited Total Required: ${(G.limitedFit.totalRequiredBytes/1024/1024/1024).toFixed(2)} GB`),console.log(`Fits in GPU (Limited): ${G.limitedFit.fitsInGpu?"Yes":"No"}`),console.log(`Fits in CPU (Limited): ${G.limitedFit.fitsInCpu?"Yes":"No"}`),console.log(`Limiting Factor (Limited): ${G.limitedFit.limiting}`)}}console.log(`
|
|
22
|
+
=== Full Capabilities JSON ===`),console.log(JSON.stringify(O,null,2)),process.exit(0)}catch(Z){console.error("Failed to get capabilities:",Z.message),process.exit(1)}}async function d2({modelIds:$=[],defaultConfig:X=null}={}){let Z=[];if(console.log(`${g.name} v${g.version}`),console.log(`Generating STT model capabilities comparison...
|
|
23
|
+
`),Z.push(`${g.name} v${g.version}`),Z.push(`## STT Model Capabilities Comparison
|
|
24
|
+
`),!$||$.length===0)console.error("Error: No model IDs provided"),process.exit(1);try{let W=(U={},Q={})=>{let K=Array.isArray(U)?[...U]:{...U};return Object.entries(Q||{}).forEach(([B,L])=>{if(L&&typeof L==="object"&&!Array.isArray(L))K[B]=W(K[B]||{},L);else K[B]=L}),K},j=X||{},{server:V,generators:Y=[],...J}=j,N=(U)=>W(JSON.parse(JSON.stringify(J)),U||{}),H=(U)=>{if(Array.isArray(Y)&&Y.length>0){let Q=Y.filter((K)=>K?.type==="ggml-stt");if(Q.length>0&&U){let K=Q.find((B)=>B.model?.repo_id===U);if(K)return N(K)}}return Object.keys(J).length>0?N({}):null},O=[];for(let U=0;U<$.length;U+=1){let Q=$[U],{repoId:K,filename:B}=p2(Q);console.log(`[${U+1}/${$.length}] Analyzing ${Q}...`);let L=H(K);L={...L||{},model:{...L?.model||{},repo_id:K,...B&&{filename:B}}};let F=await t("ggml-stt",null,{config:L,includeBreakdown:!0});O.push({modelId:Q,repoId:K,filename:B,capabilities:F,modelInfo:F.buttress?.selected||null,modelConfig:F.modelConfig||null})}let q=(U)=>U?(U/1024/1024).toFixed(1):"N/A",_=(U)=>U?"✅":"\uD83D\uDEAB";Z.push("| Model | Size (MB) | Processing Buffer (MB) | Total Required (MB) | Fits GPU | Fits CPU |"),Z.push("|-------|-----------|------------------------|---------------------|----------|----------|"),O.forEach(({modelId:U,modelInfo:Q})=>{let K=q(Q?.modelBytes),B=q(Q?.processingBytes||Q?.kvCacheBytes),L=q(Q?.fit?.totalRequiredBytes),F=_(Q?.fit?.fitsInGpu),E=_(Q?.fit?.fitsInCpu);Z.push(`| ${U} | ${K} | ${B} | ${L} | ${F} | ${E} |`)}),Z.push(`
|
|
25
|
+
---`),Z.push(`
|
|
26
|
+
### System Information`);let G=null;if(process.platform!=="win32")try{G=Y4("uname -a",{encoding:"utf8"}).trim()}catch{}if(G)Z.push(`- **System:** ${G}`);else Z.push(`- **Hostname:** ${m.hostname()}`),Z.push(`- **OS:** ${m.type()} ${m.release()}`);if(Z.push(`- **Platform:** ${process.platform}`),Z.push(`- **CPU Cores:** ${m.cpus().length}`),Z.push(`- **Total System Memory:** ${(m.totalmem()/1024/1024/1024).toFixed(2)} GB`),O.length>0){let Q=O[0].capabilities.buttress?.selected;if(Q){let K=Q.cpuTotalBytes>0?(Q.cpuUsableBytes/Q.cpuTotalBytes*100).toFixed(0):0;if(Z.push(`- **Usable CPU Memory:** ${(Q.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${K}% of ${(Q.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),Q.hasGpu){let B=Q.devices.filter((L)=>L.type==="gpu");if(B.length>0){let L=B[0];Z.push(`- **GPU Backend:** ${L.backend}`),Z.push(`- **GPU Name:** ${L.deviceName}`),Z.push(`- **GPU Total Memory:** ${(L.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let F=Q.gpuTotalBytes>0?(Q.gpuUsableBytes/Q.gpuTotalBytes*100).toFixed(0):0;Z.push(`- **GPU Usable Memory:** ${(Q.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${F}% of ${(Q.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`)}}else Z.push("- **GPU:** Not available")}}Z.push(`
|
|
27
|
+
### Command Used`);let w=process.argv.slice(2).join(" ");if(Z.push(`\`\`\`bash
|
|
28
|
+
${process.argv[0]} ${process.argv[1]} ${w}
|
|
29
|
+
\`\`\``),Z.push(`
|
|
30
|
+
### Package Information`),Z.push(`- **Name:** ${g.name}`),Z.push(`- **Version:** ${g.version}`),g.description)Z.push(`- **Description:** ${g.description}`);if(X&&Object.keys(X).length>0){Z.push(`
|
|
31
|
+
### Configuration`),Z.push("<details>"),Z.push("<summary>Click to expand TOML configuration</summary>"),Z.push("\n```toml");try{let U=g2.stringify(X);Z.push(U)}catch(U){Z.push("# Error serializing config"),Z.push(JSON.stringify(X,null,2))}Z.push("```"),Z.push("</details>")}let A=`ggml-stt-model-capabilities-${new Date().toISOString().replace(/[.:]/g,"-").split("T")[0]}.md`,R=f2.join(process.cwd(),A);u2.writeFileSync(R,Z.join(`
|
|
32
|
+
`),"utf8"),console.log(`
|
|
33
|
+
STT model capabilities table saved to: ${R}`),process.exit(0)}catch(W){console.error("Failed to generate STT model table:",W.message),process.exit(1)}}async function l2({modelId:$=null,defaultConfig:X=null}={}){if(console.log(`${g.name} v${g.version}`),console.log("Testing capabilities for backend: ggml-stt"),$)console.log(`Model: ${$}`);console.log("--------------------------------");try{let{repoId:Z,filename:W}=p2($),j=X||{},{server:V,generators:Y=[],...J}=j,N=(z={},A={})=>{let R=Array.isArray(z)?[...z]:{...z};return Object.entries(A||{}).forEach(([U,Q])=>{if(Q&&typeof Q==="object"&&!Array.isArray(Q))R[U]=N(R[U]||{},Q);else R[U]=Q}),R},H=(z)=>N(JSON.parse(JSON.stringify(J)),z||{}),q=((z)=>{if(Array.isArray(Y)&&Y.length>0){let A=Y.filter((R)=>R?.type==="ggml-stt");if(A.length>0){if(z){let R=A.find((U)=>U.model?.repo_id===z);if(R)return H(R)}}}if(Object.keys(J).length>0)return H({});return null})(Z);if(Z)q={...q||{},model:{...J.runtime,...q?.model||{},repo_id:Z,...W&&{filename:W}}};let _=await t("ggml-stt",null,{config:q,includeBreakdown:!0}),G=_.buttress?.selected||null,w=_.modelConfig||null;if(Z||w?.repoId){console.log(`
|
|
34
|
+
=== Model Information ===`);let z=Z||w?.repoId;if(console.log(`Repository ID: ${z}`),W)console.log(`Filename: ${W}`);if(G?.modelBytes)console.log(`Model Size: ${(G.modelBytes/1024/1024).toFixed(1)} MB`);let A=G?.processingBytes||G?.kvCacheBytes;if(A)console.log(`Processing Buffer: ${(A/1024/1024).toFixed(1)} MB`);if(G?.modelBytes&&A)console.log(`Total Required Memory: ${((G.modelBytes+A)/1024/1024).toFixed(1)} MB`);else if(_.buttress?.selected?.fit){let{totalRequiredBytes:R}=_.buttress.selected.fit;console.log(`Total Required Memory: ${(R/1024/1024).toFixed(1)} MB`)}}if(_.buttress?.selected){let{selected:z}=_.buttress;console.log(`
|
|
35
|
+
=== Hardware Information ===`);let A=null;if(process.platform!=="win32")try{A=Y4("uname -a",{encoding:"utf8"}).trim()}catch{}if(A)console.log(`System: ${A}`);else console.log(`Hostname: ${m.hostname()}`),console.log(`OS: ${m.type()} ${m.release()}`);console.log(`Platform: ${z.platform}`),console.log(`CPU Cores: ${m.cpus().length}`),console.log(`Total System Memory: ${(m.totalmem()/1024/1024/1024).toFixed(2)} GB`);let R=z.cpuTotalBytes>0?(z.cpuUsableBytes/z.cpuTotalBytes*100).toFixed(0):0;if(console.log(`Usable CPU Memory: ${(z.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${R}% of ${(z.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),z.hasGpu)console.log(`
|
|
36
|
+
--- GPU Details ---`),z.devices.filter((Q)=>Q.type==="gpu").forEach((Q)=>{console.log(`GPU Backend: ${Q.backend}`),console.log(`GPU Name: ${Q.deviceName}`),console.log(`GPU Total Memory: ${(Q.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let K=z.gpuTotalBytes>0?(z.gpuUsableBytes/z.gpuTotalBytes*100).toFixed(0):0;if(console.log(`GPU Usable Memory: ${(z.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${K}% of ${(z.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),Q.metadata){if(Q.metadata.hasBFloat16)console.log("Supports BFloat16: Yes");if(Q.metadata.hasUnifiedMemory)console.log("Unified Memory: Yes")}});else console.log("GPU: Not available");if(console.log(`
|
|
37
|
+
Backend Variant: ${z.variant}`),console.log(`Performance Score: ${z.score}`),z.fit)console.log(`
|
|
38
|
+
--- Model Fit Analysis ---`),console.log(`Fits in GPU: ${z.fit.fitsInGpu?"Yes":"No"}`),console.log(`Fits in CPU: ${z.fit.fitsInCpu?"Yes":"No"}`),console.log(`Limiting Factor: ${z.fit.limiting}`)}console.log(`
|
|
39
|
+
=== Full Capabilities JSON ===`),console.log(JSON.stringify(_,null,2)),process.exit(0)}catch(Z){console.error("Failed to get capabilities:",Z.message),process.exit(1)}}var p2=($)=>{if(!$)return{repoId:null,filename:null};let[X,Z]=$.split(":");return{repoId:X,filename:Z||null}};var i2=P(()=>{M0();Z1();y2()});var J4={};i3(J4,{testGgmlSttCapabilities:()=>l2,testGgmlLlmCapabilities:()=>c2,status:()=>V9,startModelDownload:()=>Q4,startGenerator:()=>W9,showSttModelsTable:()=>d2,showModelsTable:()=>m2,globalDownloadManager:()=>W1,ggmlStt:()=>Q9,ggmlLlm:()=>Y9,getModelIdentifier:()=>J9,getCapabilities:()=>t,generatorRegistry:()=>X0,finalizeGenerator:()=>j9});async function W9($,X){let W={"ggml-llm":{create:K2,getId:d4},"ggml-stt":{create:h2,getId:$1}}[$];if(!W)throw Error(`Unsupported backend type: ${$}`);let j=W.getId(X);if(!j)throw Error("Buttress generator config missing repo identifier");let V=`${$}:${j}`,Y=X0.get(V);if(Y)return Y.refCount+=1,Y.instance.resetFinalized?.(),{id:Y.id,info:Y.instance.info};let J=await W.create(V,X,{globalDownloadManager:W1}),N={id:V,type:J.type,instance:J,refCount:1};return X0.set(V,N),{id:V,info:J.info}}async function j9($){let X=X0.get($);if(!X)return!1;if(X.refCount-=1,X.refCount<=0){if(await X.instance.finalize(),!(X.instance.hasPendingReleases?.()??!1))X0.delete($)}return!0}function J9($,X){if($==="ggml-llm")return d4(X);if($==="ggml-stt")return $1(X);return null}async function Q4($,X,Z={}){let j={"ggml-llm":q2,"ggml-stt":I2}[$];if(!j)return{started:!1,localPath:null,repoId:null,error:`Unknown backend type: ${$}`};return j(X,W1,Z)}var X0,W1,Z9=($)=>{let X=X0.get($);if(!X)throw Error(`Unknown generator id "${$}"`);return X},O0=($,X)=>{let Z=Z9($);if(Z.type!==X)throw Error(`Generator "${$}" does not support ${X} backend`);return Z.instance},Y9,Q9,V9;var V4=P(()=>{l4();X1();Z1();s0();i2();X0=new Map,W1={downloads:new Map,getDownload($){return this.downloads.get($)||null},setDownload($,X){this.downloads.set($,X)},deleteDownload($){this.downloads.delete($)},isDownloading($){return this.downloads.has($)},getActiveDownloads(){return Array.from(this.downloads.entries()).map(([$,X])=>({localPath:$,promise:X}))}};Y9={async initContext($,X){return O0($,"ggml-llm").initContext(X)},async completion($,X){return O0($,"ggml-llm").completion(X)},async tokenize($,X){return O0($,"ggml-llm").tokenize(X)},async detokenize($,X){return O0($,"ggml-llm").detokenize(X)},async applyChatTemplate($,X){return O0($,"ggml-llm").applyChatTemplate(X)},async releaseContext($,X){let Z=X0.get($);if(!Z)return{released:!0,alreadyReleased:!0};if(Z.type!=="ggml-llm")throw Error(`Generator "${$}" does not support ggml-llm backend`);return Z.instance.releaseContext(X)}},Q9={async initContext($,X){return O0($,"ggml-stt").initContext(X)},async transcribe($,X){return O0($,"ggml-stt").transcribe(X)},async transcribeData($,X){return O0($,"ggml-stt").transcribeData(X)},async releaseContext($,X){let Z=X0.get($);if(!Z)return{released:!0,alreadyReleased:!0};if(Z.type!=="ggml-stt")throw Error(`Generator "${$}" does not support ggml-stt backend`);return Z.instance.releaseContext(X)}};V9={getFullStatus:()=>d1(X0),getGgmlLlmStatus:()=>C4(X0),getGgmlSttStatus:()=>y4(X0),subscribeToStatus:I4,subscribeToStatusWithId:c1,llmStatusTracker:$0,sttStatusTracker:N0,statusEmitter:e}});import{node as N9}from"@elysiajs/node";import{Elysia as U9}from"elysia";var G9,K0=($)=>new U9({adapter:G9?N9():void 0,...$});var k0=P(()=>{G9=typeof process<"u"&&process.versions&&process.versions.node});import{t as Y0}from"elysia";var H9,O9=({store:{serverInfo:$}})=>({id:$.id,name:$.name,version:$.version,generators:$.generators,authentication:$.authentication}),j1=($)=>{let X=K0(),Z=$.autodiscover.http?.path??"/buttress/info";return X.get(Z,O9,{response:H9}),X};var s2=P(()=>{k0();H9=Y0.Object({id:Y0.String(),name:Y0.String(),version:Y0.String(),generators:Y0.Array(Y0.Object({type:Y0.String()})),authentication:Y0.Object({required:Y0.Boolean(),type:Y0.Literal("device-group")})})});import{t as Q0,file as K9}from"elysia";import{writeFile as n2}from"node:fs/promises";import Y1 from"node:path";var q9,Q1;var r2=P(()=>{k0();q9=typeof process<"u"&&process.versions!=null&&process.versions.node!=null,Q1=K0().post("/buttress/upload",async({body:{file:$},store:{config:X}})=>{let Z=`${Date.now()}-${$.name.replace(/[^\dA-Za-z]/g,"_")}`,W=Y1.join(X.server.temp_file_dir,Z);try{if(q9)await n2(W,await $.stream());else await n2(W,await $.arrayBuffer());return{ok:!0,filename:Z}}catch(j){return{ok:!1,error:String(j)}}},{body:Q0.Object({file:Q0.File()}),response:Q0.Object({ok:Q0.Boolean(),filename:Q0.Optional(Q0.String()),error:Q0.Optional(Q0.String())})}).get("/buttress/download/:filename",async({params:{filename:$},store:{config:X},status:Z})=>{let W=Y1.join(X.server.temp_file_dir,$);if(Y1.relative(X.server.temp_file_dir,W).includes(".."))return Z(400),"Invalid file path";return K9(W)},{params:Q0.Object({filename:Q0.String()})})});import J1 from"node:path";import t2 from"node:fs/promises";import{fileURLToPath as _9}from"node:url";var o2,L9=async()=>{let $=[J1.join(o2,"..","public","status.html"),J1.join(o2,"..","..","public","status.html")];return(await Promise.all($.map((Z)=>t2.access(Z).then(()=>Z,()=>null)))).find((Z)=>Z!==null)??null},A9=($)=>{let{status:X}=$;if(X?.getFullStatus)return X.getFullStatus();return{timestamp:new Date().toISOString(),ggmlLlm:{generators:[],history:{}},ggmlStt:{generators:[],history:{}}}},a2=async()=>{let $=await L9();if(!$)return console.error("[Status] Failed to find status.html in candidate paths"),new Response("Status page not found",{status:404,headers:{"Content-Type":"text/plain"}});try{let X=await t2.readFile($,"utf-8");return new Response(X,{headers:{"Content-Type":"text/html; charset=utf-8"}})}catch(X){return console.error("[Status] Failed to serve status page:",X),new Response("Status page not found",{status:404,headers:{"Content-Type":"text/plain"}})}},V1;var e2=P(()=>{k0();o2=J1.dirname(_9(import.meta.url)),V1=K0().get("/status",a2).get("/status/",a2).get("/buttress/status",({store:{backend:$}})=>A9($))});import{t as T,sse as N4}from"elysia";import{cors as z9}from"@elysiajs/cors";async function R9($,X,Z){let j=(X.generators||[]).filter((z)=>z.type==="ggml-llm");if(j.length===0)throw Error('No ggml-llm generator configured. Add a [[generators]] with type = "ggml-llm" to your config.');let V=j[0],Y=Z||V.model?.repo_id;if(Z){let z=j.find((A)=>A.model?.repo_id===Z);if(z)V=z}else Y=V.model?.repo_id;let J=Y,N=$3.get(J);if(N?.initialized)return N;let{generators:H,server:O,...q}=X.global||{},_={...q,...V,model:{...V.model,repo_id:Y}};console.log(`[OpenAI] Creating generator for ${J}`);let{id:G}=await $.startGenerator("ggml-llm",_),w={id:G,config:_,repoId:Y,initialized:!1};return $3.set(J,w),await $.ggmlLlm.initContext(G,{}),w.initialized=!0,console.log(`[OpenAI] Generator ready: ${J}`),w}async function w9($,X,Z,W){let j=$.getReader(),V="",Y=null,J=null,N="stop",H={prompt_tokens:0,completion_tokens:0,total_tokens:0};try{let q=!1;while(!q){let _=await j.read();if({done:q}=_,q)break;let{event:G,data:w}=_.value;if(G==="token"){if(w.content!=null)V+=w.content;else if(w.token!=null)V+=w.token}else if(G==="result"){if(w.text)V=w.text;else if(w.content)V=w.content;if(w.reasoning_content)Y=w.reasoning_content;if(w.tool_calls?.length>0)J=w.tool_calls.map((z,A)=>({id:z.id||`call_${X}_${A}`,type:"function",function:{name:z.function?.name||"",arguments:z.function?.arguments||""}})),N="tool_calls";else N=w.interrupted?"length":"stop";H={prompt_tokens:w.prompt_tokens||w.promptTokens||0,completion_tokens:w.tokens_predicted||w.tokensPredicted||0,total_tokens:(w.prompt_tokens||w.promptTokens||0)+(w.tokens_predicted||w.tokensPredicted||0)}}else if(G==="error")throw Error(w.message)}}finally{j.cancel().catch(()=>{})}let O={role:"assistant",content:V||null};if(Y)O.reasoning_content=Y;if(J)O.tool_calls=J;return{id:X,object:"chat.completion",created:Z,model:W,choices:[{index:0,message:O,finish_reason:N}],usage:H}}function U4({global:$}){let X=K0({prefix:"/oai-compat"});return X.use(z9({origin:$?.openai_compat?.cors_allowed_origins??!1,methods:["GET","POST","OPTIONS"],allowedHeaders:["Content-Type","Authorization"],maxAge:86400,preflight:!0})),X.get("/v1/models",({store:Z})=>{let{config:W}=Z,Y=(W.generators||[]).filter((J)=>J.type==="ggml-llm").map((J)=>{return{id:J.model?.repo_id||"ggml-llm",object:"model",created:Math.floor(Date.now()/1000),owned_by:"local"}});if(Y.length===0)Y.push({id:"ggml-llm",object:"model",created:Math.floor(Date.now()/1000),owned_by:"local"});return{object:"list",data:Y}}),X.post("/v1/chat/completions",async function*({body:W,set:j,store:V}){let{config:Y,backend:J}=V,{messages:N=[],stream:H=!1,model:O,tools:q,temperature:_,stop:G,top_p:w,max_tokens:z,presence_penalty:A,frequency_penalty:R,tool_choice:U,stream_options:Q}=W;if(!N||N.length===0)return j.status=400,{error:{message:"messages is required and must not be empty",type:"invalid_request_error"}};try{let K=await R9(J,Y,O),B=B9(),L=Math.floor(Date.now()/1000),F=K.repoId||"ggml-llm",E={reasoning_format:"auto",messages:N,jinja:!0,add_generation_prompt:!0};if(_!=null)E.temperature=_;if(w!=null)E.top_p=w;if(z!=null)E.n_predict=z;if(G!=null)E.stop=Array.isArray(G)?G:[G];if(A!=null)E.presence_penalty=A;if(R!=null)E.frequency_penalty=R;if(q!=null)E.tools=q;if(U!=null)E.tool_choice=U;E.enable_thinking=!1;let M=await J.ggmlLlm.completion(K.id,{options:E});if(!H)return await w9(M,B,L,F);let x=Q?.include_usage===!0,D=M.getReader(),I="",b="",u=new Map,r=new Map;try{let k=!1;while(!k){let d=await D.read();if({done:k}=d,k)break;let{event:v,data:S}=d.value;if(v==="token"){let C={};if(S.content!=null){let h=S.content;if(h.length>I.length)C.content=h.slice(I.length),I=h}if(S.reasoning_content!=null){let h=S.reasoning_content;if(h.length>b.length)C.reasoning_content=h.slice(b.length),b=h}if(S.tool_calls?.length>0){let h=[];if(S.tool_calls.forEach((p,f)=>{let s={index:f};if(!r.has(f))r.set(f,p.id||`call_${B}_${f}`),s.id=r.get(f),s.type="function";let W0=p.function?.arguments||"",A0=u.get(f)||"",l={};if(!u.has(f)&&p.function?.name)l.name=p.function.name;if(W0.length>A0.length)l.arguments=W0.slice(A0.length),u.set(f,W0);if(Object.keys(l).length>0)s.function=l,h.push(s);else if(s.id)s.function={name:p.function?.name||"",arguments:""},h.push(s)}),h.length>0)C.tool_calls=h}if(Object.keys(C).length>0)yield N4({data:JSON.stringify({id:B,object:"chat.completion.chunk",created:L,model:F,choices:[{index:0,delta:C,finish_reason:null}]})})}else if(v==="result"){let C="stop";if(S.tool_calls?.length>0||r.size>0)C="tool_calls";else if(S.interrupted)C="length";let h={id:B,object:"chat.completion.chunk",created:L,model:F,choices:[{index:0,delta:{},finish_reason:C}]};if(x)h.usage={prompt_tokens:S.prompt_tokens||S.promptTokens||0,completion_tokens:S.tokens_predicted||S.tokensPredicted||0,total_tokens:(S.prompt_tokens||S.promptTokens||0)+(S.tokens_predicted||S.tokensPredicted||0)};yield N4({data:JSON.stringify(h)})}else if(v==="error")yield N4({data:JSON.stringify({error:{message:S.message,type:"server_error"}})})}yield N4({data:"[DONE]"})}finally{D.cancel().catch(()=>{})}}catch(K){return console.error("[OpenAI] Chat completion error:",K),j.status=500,{error:{message:K.message||"Internal server error",type:"server_error"}}}},{body:T.Object({model:T.Optional(T.String()),messages:T.Array(T.Any()),stream:T.Optional(T.Boolean()),temperature:T.Optional(T.Number()),top_p:T.Optional(T.Number()),max_tokens:T.Optional(T.Number()),stop:T.Optional(T.Union([T.String(),T.Array(T.String())])),presence_penalty:T.Optional(T.Number()),frequency_penalty:T.Optional(T.Number()),tools:T.Optional(T.Array(T.Any())),tool_choice:T.Optional(T.Any()),stream_options:T.Optional(T.Object({include_usage:T.Optional(T.Boolean())}))})}),X}var B9=()=>`chatcmpl-${Date.now()}-${Math.random().toString(36).slice(2,9)}`,$3;var X3=P(()=>{k0();$3=new Map});var Z3=P(()=>{s2();r2();e2();X3()});import E9 from"node:os";import M9 from"node:path";import x9 from"node-machine-id";import S9 from"bytes";import P9 from"ms";var D0=($={},X={})=>{let Z=Array.isArray($)?[...$]:{...$};return Object.entries(X||{}).forEach(([W,j])=>{if(j&&typeof j==="object"&&!Array.isArray(j))Z[W]=D0(Z[W]||{},j);else Z[W]=j}),Z},w0=($)=>{if(!$)return null;if(typeof $==="object")return JSON.parse(JSON.stringify($));return null},N1=($,X)=>{let Z=w0($)||{},W=w0(X)||{};return D0(Z,W)},W3=($,X)=>D0(JSON.parse(JSON.stringify($.global)),X||{}),U1=($,X,Z,W)=>{if($.generators.length>0){let j=$.generators.filter((V)=>V?.type===Z);if(j.length>0){if(W){let V=j.find((Y)=>X.getModelIdentifier(Z,Y)===W);if(V)return W3($,V)}}}if(Object.keys($.global).length>0)return W3($,{});return null},j3,T9=($)=>{if(!$)return null;if($===!0)return{...j3};return D0(j3,$)},V3=($,X)=>{if(!$.generators||$.generators.length===0)return X.map((W)=>({type:W}));let Z=new Set;if($.generators.forEach((W)=>{if(W.type)Z.add(W.type)}),Z.size===0)return X.map((W)=>({type:W}));return Array.from(Z).map((W)=>({type:W}))},Y3=($,X,Z)=>{if($===void 0)return Z;if(typeof $==="number")return $;return X($)??Z},k9=2080,Q3=60000,J3=52428800,G1=($)=>{let X=x9.machineIdSync(),Z={server:{id:`buttress-${X}`,name:`Buttress Server (${X.slice(-8)})`,port:k9,temp_file_dir:M9.join(E9.tmpdir(),".buttress"),session_timeout:Q3,max_body_size:J3},autodiscover:!1},W=D0(Z,w0($)||{}),j=Array.isArray(W.generators)?W.generators:[],{server:V,generators:Y,autodiscover:J,...N}=W;return{autodiscover:T9(J),server:{id:V.id,name:V.name,port:V.port,log_level:V.log_level,temp_file_dir:V.temp_file_dir,max_body_size:Y3(V.max_body_size,S9.parse,J3),session_timeout:Y3(V.session_timeout,P9,Q3)},global:N,generators:j}};var b0=P(()=>{j3={udp:{port:8089,announcements:{enabled:!0,interval:5000},requests:{enabled:!0,responseDelay:100}},http:{enabled:!0,path:"/buttress/info",cors:!0}}});import{z as Z0}from"zod";var N3,U3;var G3=P(()=>{b0();N3={getCapabilities:Z0.tuple([Z0.object({type:Z0.string().optional().default("ggml-llm"),config:Z0.any().optional(),currentClientCapabilities:Z0.any().optional(),options:Z0.any().optional()}).nullable().optional()]),startGenerator:Z0.tuple([Z0.string(),Z0.any().optional()]),finalizeGenerator:Z0.tuple([Z0.string()])},U3={async getCapabilities({backend:$,config:X},Z=null){console.log("[Server] Get Capabilities:",Z);let W=Z||{type:"ggml-llm"},{type:j="ggml-llm",config:V,currentClientCapabilities:Y=null,options:J={}}=W,N=w0(V),H=$.getModelIdentifier(j,N),O=U1(X,$,j,H),q=N1(O,V);if(Object.keys(q).length===0)throw Error("Buttress server missing generator configuration");if(q.backend=q.backend||{},!q.backend.type)q.backend.type=j;return $.getCapabilities(j,Y,{...J,config:q})},async startGenerator({backend:$,config:X,session:Z},W,j){console.log("[Server] Start Generator:",W,j);let V=w0(j),Y=$.getModelIdentifier(W,V),J=U1(X,$,W,Y),N=N1(J,j);if(Object.keys(N).length===0)throw Error("Buttress server missing generator configuration");if(N.backend=N.backend||{},!N.backend.type)N.backend.type=W;let H=await $.startGenerator(W,N);return Z.generators.add(H.id),H},async finalizeGenerator({backend:$,session:X},Z){return console.log("[Server] Finalize Generator:",Z),X.generators.delete(Z),$.finalizeGenerator(Z)}}});import{z as i}from"zod";import{ReadableStream as D9}from"node:stream/web";var H3,O3;var K3=P(()=>{H3={initContext:i.tuple([i.string(),i.any().optional()]),completion:i.tuple([i.string(),i.any().optional()]),tokenize:i.tuple([i.string(),i.any()]),detokenize:i.tuple([i.string(),i.any()]),applyChatTemplate:i.tuple([i.string(),i.any()]),releaseContext:i.tuple([i.string()])},O3={initContext({backend:$,session:X},Z,W){return new D9({async start(j){try{let V=await $.ggmlLlm.initContext(Z,{...W,onProgress:(N)=>{j.enqueue({event:"progress",data:{progress:N}})}});X.initializedContexts.add(Z),await new Promise((N)=>setTimeout(N,1000));let{download:Y,...J}=V||{};j.enqueue({event:"result",data:{result:J}}),j.close()}catch(V){j.error(V)}}})},completion({backend:$},X,Z){return console.log("[Server] Completion:",{id:X,property:Z}),$.ggmlLlm.completion(X,Z)},async tokenize({backend:$},X,Z){return console.log("[Server] Tokenize:",{id:X,property:Z}),$.ggmlLlm.tokenize(X,Z)},async detokenize({backend:$},X,Z){return console.log("[Server] Detokenize:",{id:X,property:Z}),$.ggmlLlm.detokenize(X,Z)},async applyChatTemplate({backend:$},X,Z){return console.log("[Server] Apply Chat Template:",{id:X,property:Z}),$.ggmlLlm.applyChatTemplate(X,Z)},async releaseContext({backend:$,session:X},Z,W){if(console.log("[Server] Release Context:",{id:Z,force:W}),!X.initializedContexts.has(Z))return console.log("[Server] Release Context skipped - not initialized by this session:",{id:Z}),{released:!1,skipped:!0};return X.initializedContexts.delete(Z),$.ggmlLlm.releaseContext(Z,{force:W})}}});import{z as a}from"zod";import{ReadableStream as b9}from"node:stream/web";import v9 from"node:path";var q3,_3;var L3=P(()=>{q3={initContext:a.tuple([a.string(),a.any().optional()]),transcribe:a.tuple([a.string(),a.string(),a.any().optional()]),transcribeData:a.tuple([a.string(),a.union([a.instanceof(Buffer),a.instanceof(Uint8Array)]),a.any().optional()]),releaseContext:a.tuple([a.string()])},_3={initContext({backend:$,session:X},Z,W){return new b9({async start(j){try{let V=await $.ggmlStt.initContext(Z,{...W,onProgress:(N)=>{j.enqueue({event:"progress",data:{progress:N}})}});X.initializedContexts.add(Z),await new Promise((N)=>setTimeout(N,1000));let{download:Y,...J}=V||{};j.enqueue({event:"result",data:{result:J}}),j.close()}catch(V){j.error(V)}}})},async transcribe({backend:$,config:{server:X}},Z,W,j){return console.log("[Server] Transcribe:",{id:Z,audioPath:W,options:j}),$.ggmlStt.transcribe(Z,{audioPath:v9.join(X.temp_file_dir,W),options:j})},async transcribeData({backend:$},X,Z,W){return console.log("[Server] Transcribe Data:",{id:X,audioDataLength:Z?.length||0,options:W}),$.ggmlStt.transcribeData(X,{audioData:Z,options:W})},async releaseContext({backend:$,session:X},Z,W){if(console.log("[Server] Release STT Context:",{id:Z,force:W}),!X.initializedContexts.has(Z))return console.log("[Server] Release STT Context skipped - not initialized by this session:",{id:Z}),{released:!1,skipped:!0};return X.initializedContexts.delete(Z),$.ggmlStt.releaseContext(Z,{force:W})}}});var h9,A3,z3;var B3=P(()=>{G3();K3();L3();h9={common:U3,ggmlLlm:O3,ggmlStt:_3},A3={common:N3,ggmlLlm:H3,ggmlStt:q3},z3=h9});import{Buffer as G4}from"node:buffer";var R3=($)=>{try{return JSON.parse($,(X,Z)=>{if(!Z)return Z;if(Z?.type==="Buffer"&&Z?.data)return G4.from(Z.data,"base64");if(Z?.type==="Uint8Array"&&Z?.data){let W=G4.from(Z.data,"base64");return W.buffer.slice(W.byteOffset,W.byteOffset+W.byteLength)}if(Z?.type==="Error"&&Z?.name&&Z?.message)return Error(Z.name,Z.message);return Z})}catch{return $}},H4=($)=>{try{return JSON.stringify($,(X,Z)=>{if(Z instanceof Error)return{type:"Error",name:Z.name,message:Z.message};if(Z instanceof G4)return{type:"Buffer",data:Z.toString("base64")};if(Z instanceof Uint8Array)return{type:"Uint8Array",data:G4.from(Z).toString("base64")};return Z})}catch{return $}};var w3=()=>{};var H1="1.0",F3=8089;import I9 from"node:dgram";class O1{name="udp";socket=null;announcementTimer=null;config;getServerInfo;port;constructor($,X){this.config=$,this.getServerInfo=X,this.port=$.port??F3}async start(){if(this.socket=I9.createSocket({type:"udp4",reuseAddr:!0}),this.socket.on("message",($,X)=>{this.handleMessage($,X)}),this.socket.on("error",($)=>{console.error("[Autodiscover UDP] Socket error:",$.message)}),await new Promise(($,X)=>{this.socket.bind(this.port,()=>{this.socket.setBroadcast(!0),console.log(`[Autodiscover UDP] Listening on port ${this.port}`),$()}),this.socket.once("error",X)}),this.config.announcements.enabled){let $=this.config.announcements.interval??5000;this.announcementTimer=setInterval(()=>{this.sendAnnouncement()},$),this.sendAnnouncement()}}async stop(){if(this.announcementTimer)clearInterval(this.announcementTimer),this.announcementTimer=null;if(this.socket)await new Promise(($)=>{this.socket.close(()=>$())}),this.socket=null}handleMessage($,X){try{let Z=JSON.parse($.toString());if(Z.t==="QUERY"&&this.config.requests.enabled){let W=Z.d,j=this.config.requests.responseDelay??0,V=j>0?Math.random()*j:0;setTimeout(()=>{this.sendResponse(W.id,X)},V)}}catch{}}sendAnnouncement(){if(!this.socket)return;let $=this.getServerInfo(),X={t:"ANNOUNCE",v:H1,d:{info:$}},Z=Buffer.from(JSON.stringify(X));this.socket.send(Z,0,Z.length,this.port,"255.255.255.255",(W)=>{if(W)console.error("[Autodiscover UDP] Announcement error:",W.message)})}sendResponse($,X){if(!this.socket)return;let Z=this.getServerInfo(),W={t:"RESPONSE",v:H1,d:{request_id:$,info:Z}},j=Buffer.from(JSON.stringify(W));this.socket.send(j,0,j.length,X.port,X.address,(V)=>{if(V)console.error("[Autodiscover UDP] Response error:",V.message)})}}var E3=()=>{};class K1{config;getServerInfo;transports=[];started=!1;constructor($,X){this.config=$;this.getServerInfo=X;if($.udp?.announcements?.enabled||$.udp?.requests?.enabled)this.transports.push(new O1($.udp,X))}async start(){if(this.started)return;(await Promise.allSettled(this.transports.map((X)=>X.start()))).forEach((X,Z)=>{if(X.status==="rejected")console.error(`[Autodiscover] Failed to start ${this.transports[Z].name}:`,X.reason)}),this.started=!0}async stop(){if(!this.started)return;await Promise.allSettled(this.transports.map(($)=>$.stop())),this.started=!1}}var M3=P(()=>{E3()});import C9 from"node:os";var O4=()=>{let $=C9.networkInterfaces();return Object.values($).flat().find((Z)=>Z?.family==="IPv4"&&!Z?.internal)?.address||null};var q1=()=>{};import c from"node:os";import x3 from"node:fs";import S3 from"node:path";import{execSync as K4}from"node:child_process";import P3 from"@iarna/toml";async function _1({modelIds:$=[],defaultConfig:X=null}={}){let Z=[];if(console.log(`${"@fugood/buttress-server"} v${"2.23.0-beta.53"}`),console.log(`Generating model capabilities comparison...
|
|
40
|
+
`),Z.push(`${"@fugood/buttress-server"} v${"2.23.0-beta.53"}`),Z.push(`## Model Capabilities Comparison
|
|
41
|
+
`),!$||$.length===0)console.error("Error: No model IDs provided"),process.exit(1);try{let W=(U={},Q={})=>{let K=Array.isArray(U)?[...U]:{...U};return Object.entries(Q||{}).forEach(([B,L])=>{if(L&&typeof L==="object"&&!Array.isArray(L))K[B]=W(K[B]||{},L);else K[B]=L}),K},j=X||{},{server:V,generators:Y=[],...J}=j,N=(U)=>W(JSON.parse(JSON.stringify(J)),U||{}),H=(U)=>{if(Array.isArray(Y)&&Y.length>0){let Q=Y.filter((K)=>K?.type==="ggml-llm");if(Q.length>0&&U){let K=Q.find((B)=>B.model?.repo_id===U);if(K)return N(K)}}return Object.keys(J).length>0?N({}):null},O=[];for(let U=0;U<$.length;U+=1){let Q=$[U];console.log(`[${U+1}/${$.length}] Analyzing ${Q}...`);let K=H(Q);K={...K||{},model:{...J.runtime,...K?.model||{},repo_id:Q}};let B=await t("ggml-llm",null,{config:K,includeBreakdown:!0});O.push({modelId:Q,capabilities:B,modelInfo:B.buttress?.selected||null,modelConfig:B.modelConfig||null})}let q=(U)=>U?(U/1024/1024/1024).toFixed(2):"N/A",_=(U)=>U?"✅":"\uD83D\uDEAB";Z.push("| Model ID | Size (GB) | Context Size | KV Cache Size (GB) | Recurrent Mem (GB) | Total Required Memory (GB) | Fits GPU (Full) | Fits CPU (Full) |"),Z.push("|----------|-----------|--------------|--------------------|--------------------|----------------------------|-----------------|-----------------|"),O.forEach(({modelId:U,modelInfo:Q,modelConfig:K})=>{let B=q(Q?.modelBytes),L=K?.nCtx||Q?.kvInfo?.nCtxTrain||"N/A",F=G0(Q),E=Number(L),M=Q?.kvCacheBytes||(F&&Number.isFinite(E)&&E>0?F(E):F&&F(Q?.kvInfo?.nCtxTrain||0))||null,x=q(M),D=Q?.recurrentMemoryBytes||0,I=D>0?q(D):"-",b=q(Q?.modelBytes&&(M!=null||D>0)?Q.modelBytes+(M||0)+D:Q?.fit?.totalRequiredBytes),u=_(Q?.fit?.fitsInGpu),r=_(Q?.fit?.fitsInCpu);Z.push(`| ${U} | ${B} | ${L} | ${x} | ${I} | ${b} | ${u} | ${r} |`);let k=Q?.memoryLimitedCtx!=null||Q?.limitedFit!=null,d=!Q?.fit?.fitsInGpu||!Q?.fit?.fitsInCpu;if(k&&d){let v=Q?.memoryLimitedCtx||L,S=Number(v),C=Q?.limitedKvCacheBytes||F&&Number.isFinite(S)&&S>0&&F(S)||null,h=q(C),p=q(Q?.modelBytes&&(C!=null||D>0)?Q.modelBytes+(C||0)+D:Q?.limitedFit?.totalRequiredBytes),f=_(Q?.limitedFit?.fitsInGpu),s=_(Q?.limitedFit?.fitsInCpu);if(v!==L||h!==x||p!==b)Z.push(`| ↳ Limited | ${B} | ${v} | ${h} | ${I} | ${p} | ${f} | ${s} |`)}}),Z.push(`
|
|
42
|
+
---`),Z.push(`
|
|
43
|
+
### System Information`);let G=null;if(process.platform!=="win32")try{G=K4("uname -a",{encoding:"utf8"}).trim()}catch{}if(G)Z.push(`- **System:** ${G}`);else Z.push(`- **Hostname:** ${c.hostname()}`),Z.push(`- **OS:** ${c.type()} ${c.release()}`);if(Z.push(`- **Platform:** ${process.platform}`),Z.push(`- **CPU Cores:** ${c.cpus().length}`),Z.push(`- **Total System Memory:** ${(c.totalmem()/1024/1024/1024).toFixed(2)} GB`),O.length>0){let Q=O[0].capabilities.buttress?.selected;if(Q){let K=Q.cpuTotalBytes>0?(Q.cpuUsableBytes/Q.cpuTotalBytes*100).toFixed(0):0;if(Z.push(`- **Usable CPU Memory:** ${(Q.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${K}% of ${(Q.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),Q.hasGpu){let B=Q.devices.filter((L)=>L.type==="gpu");if(B.length>0){let L=B[0];Z.push(`- **GPU Backend:** ${L.backend}`),Z.push(`- **GPU Name:** ${L.deviceName}`),Z.push(`- **GPU Total Memory:** ${(L.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let F=Q.gpuTotalBytes>0?(Q.gpuUsableBytes/Q.gpuTotalBytes*100).toFixed(0):0;Z.push(`- **GPU Usable Memory:** ${(Q.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${F}% of ${(Q.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`)}}else Z.push("- **GPU:** Not available")}}Z.push(`
|
|
44
|
+
### Command Used`);let w=process.argv.slice(2).join(" ");if(Z.push(`\`\`\`bash
|
|
45
|
+
${process.argv[0]} ${process.argv[1]} ${w}
|
|
46
|
+
\`\`\``),Z.push(`
|
|
47
|
+
### Package Information`),Z.push(`- **Name:** ${"@fugood/buttress-server"}`),Z.push(`- **Version:** ${"2.23.0-beta.53"}`),X&&Object.keys(X).length>0){Z.push(`
|
|
48
|
+
### Configuration`),Z.push("<details>"),Z.push("<summary>Click to expand TOML configuration</summary>"),Z.push("\n```toml");try{let U=P3.stringify(X);Z.push(U)}catch(U){Z.push("# Error serializing config"),Z.push(JSON.stringify(X,null,2))}Z.push("```"),Z.push("</details>")}let A=`ggml-llm-model-capabilities-${new Date().toISOString().replace(/[.:]/g,"-").split("T")[0]}.md`,R=S3.join(process.cwd(),A);x3.writeFileSync(R,Z.join(`
|
|
49
|
+
`),"utf8"),console.log(`
|
|
50
|
+
Model capabilities table saved to: ${R}`),process.exit(0)}catch(W){console.error("Failed to generate model table:",W.message),process.exit(1)}}async function k3({modelId:$=null,defaultConfig:X=null}={}){if(console.log(`${"@fugood/buttress-server"} v${"2.23.0-beta.53"}`),console.log("Testing capabilities for backend: ggml-llm"),$)console.log(`Model: ${$}`);console.log("--------------------------------");try{let Z=X||{},{server:W,generators:j=[],...V}=Z,Y=(G={},w={})=>{let z=Array.isArray(G)?[...G]:{...G};return Object.entries(w||{}).forEach(([A,R])=>{if(R&&typeof R==="object"&&!Array.isArray(R))z[A]=Y(z[A]||{},R);else z[A]=R}),z},J=(G)=>Y(JSON.parse(JSON.stringify(V)),G||{}),H=((G)=>{if(Array.isArray(j)&&j.length>0){let w=j.filter((z)=>z?.type==="ggml-llm");if(w.length>0){if(G){let z=w.find((A)=>A.model?.repo_id===G);if(z)return J(z)}}}if(Object.keys(V).length>0)return J({});return null})($);if($)H={...H||{},model:{...H?.model||{},repo_id:$}};let O=await t("ggml-llm",null,{config:H,includeBreakdown:!0}),q=O.buttress?.selected||null,_=O.modelConfig||null;if($||_?.repoId){console.log(`
|
|
51
|
+
=== Model Information ===`);let G=$||_?.repoId;if(console.log(`Repository ID: ${G}`),_?.quantization)console.log(`Quantization: ${_.quantization}`);if(_?.nCtx)console.log(`Context Length: ${_.nCtx}`);if(q?.quantization){let{fileType:A}=q.quantization;if(A!=null)console.log(`Model File Type (GGUF): ${A}`)}let w=_?.cache_type_k||"f16",z=_?.cache_type_v||"f16";if(console.log(`KV Cache Type: K=${w}, V=${z}`),q?.modelBytes&&q?.kvCacheBytes!=null){if(console.log(`Model Size: ${(q.modelBytes/1024/1024/1024).toFixed(2)} GB`),q.kvInfo)console.log(`KV Cache Size: ${(q.kvCacheBytes/1024/1024/1024).toFixed(2)} GB (KV info: ${JSON.stringify(q.kvInfo)})`);else console.log(`KV Cache Size: ${(q.kvCacheBytes/1024/1024/1024).toFixed(2)} GB`);let A=q.recurrentMemoryBytes||0;if(A>0)console.log(`Recurrent Memory: ${(A/1024/1024/1024).toFixed(2)} GB`);let R=q.modelBytes+q.kvCacheBytes+A;if(console.log(`Total Required Memory: ${(R/1024/1024/1024).toFixed(2)} GB`),q.memoryLimitedCtx!=null){let U=q.memoryLimitedCtx,Q=q.kvInfo?.nCtxTrain;if(Q)console.log(`
|
|
52
|
+
Memory-Limited Context: ${U} (Train: ${Q})`);else console.log(`
|
|
53
|
+
Memory-Limited Context: ${U}`);if(q.limitedKvCacheBytes!=null)console.log(`Limited KV Cache Size: ${(q.limitedKvCacheBytes/1024/1024/1024).toFixed(2)} GB`)}}else if(O.buttress?.selected?.fit){let{totalRequiredBytes:A}=O.buttress.selected.fit;console.log(`Total Required Memory: ${(A/1024/1024/1024).toFixed(2)} GB`)}}if(O.buttress?.selected){let{selected:G}=O.buttress;console.log(`
|
|
54
|
+
=== Hardware Information ===`);let w=null;if(process.platform!=="win32")try{w=K4("uname -a",{encoding:"utf8"}).trim()}catch{}if(w)console.log(`System: ${w}`);else console.log(`Hostname: ${c.hostname()}`),console.log(`OS: ${c.type()} ${c.release()}`);console.log(`Platform: ${G.platform}`),console.log(`CPU Cores: ${c.cpus().length}`),console.log(`Total System Memory: ${(c.totalmem()/1024/1024/1024).toFixed(2)} GB`);let z=G.cpuTotalBytes>0?(G.cpuUsableBytes/G.cpuTotalBytes*100).toFixed(0):0;if(console.log(`Usable CPU Memory: ${(G.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${z}% of ${(G.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),G.hasGpu)console.log(`
|
|
55
|
+
--- GPU Details ---`),G.devices.filter((R)=>R.type==="gpu").forEach((R)=>{console.log(`GPU Backend: ${R.backend}`),console.log(`GPU Name: ${R.deviceName}`),console.log(`GPU Total Memory: ${(R.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let U=G.gpuTotalBytes>0?(G.gpuUsableBytes/G.gpuTotalBytes*100).toFixed(0):0;if(console.log(`GPU Usable Memory: ${(G.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${U}% of ${(G.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),R.metadata){if(R.metadata.hasBFloat16)console.log("Supports BFloat16: Yes");if(R.metadata.hasUnifiedMemory)console.log("Unified Memory: Yes")}});else console.log("GPU: Not available");if(console.log(`
|
|
56
|
+
Backend Variant: ${G.variant}`),console.log(`Performance Score: ${G.score}`),G.fit){if(console.log(`
|
|
57
|
+
--- Model Fit Analysis ---`),console.log(`Fits in GPU: ${G.fit.fitsInGpu?"Yes":"No"}`),console.log(`Fits in CPU: ${G.fit.fitsInCpu?"Yes":"No"}`),console.log(`Limiting Factor: ${G.fit.limiting}`),G.limitedFit)console.log(`
|
|
58
|
+
--- Memory-Limited Fit Analysis ---`),console.log(`Limited Total Required: ${(G.limitedFit.totalRequiredBytes/1024/1024/1024).toFixed(2)} GB`),console.log(`Fits in GPU (Limited): ${G.limitedFit.fitsInGpu?"Yes":"No"}`),console.log(`Fits in CPU (Limited): ${G.limitedFit.fitsInCpu?"Yes":"No"}`),console.log(`Limiting Factor (Limited): ${G.limitedFit.limiting}`)}}console.log(`
|
|
59
|
+
=== Full Capabilities JSON ===`),console.log(JSON.stringify(O,null,2)),process.exit(0)}catch(Z){console.error("Failed to get capabilities:",Z.message),process.exit(1)}}async function L1({modelIds:$=[],defaultConfig:X=null}={}){let Z=[];if(console.log(`${"@fugood/buttress-server"} v${"2.23.0-beta.53"}`),console.log(`Generating STT model capabilities comparison...
|
|
60
|
+
`),Z.push(`${"@fugood/buttress-server"} v${"2.23.0-beta.53"}`),Z.push(`## STT Model Capabilities Comparison
|
|
61
|
+
`),!$||$.length===0)console.error("Error: No model IDs provided"),process.exit(1);try{let W=(U={},Q={})=>{let K=Array.isArray(U)?[...U]:{...U};return Object.entries(Q||{}).forEach(([B,L])=>{if(L&&typeof L==="object"&&!Array.isArray(L))K[B]=W(K[B]||{},L);else K[B]=L}),K},j=X||{},{server:V,generators:Y=[],...J}=j,N=(U)=>W(JSON.parse(JSON.stringify(J)),U||{}),H=(U)=>{if(Array.isArray(Y)&&Y.length>0){let Q=Y.filter((K)=>K?.type==="ggml-stt");if(Q.length>0&&U){let K=Q.find((B)=>B.model?.repo_id===U);if(K)return N(K)}}return Object.keys(J).length>0?N({}):null},O=[];for(let U=0;U<$.length;U+=1){let Q=$[U],{repoId:K,filename:B}=T3(Q);console.log(`[${U+1}/${$.length}] Analyzing ${Q}...`);let L=H(K);L={...L||{},model:{...L?.model||{},repo_id:K,...B&&{filename:B}}};let F=await t("ggml-stt",null,{config:L,includeBreakdown:!0});O.push({modelId:Q,repoId:K,filename:B,capabilities:F,modelInfo:F.buttress?.selected||null,modelConfig:F.modelConfig||null})}let q=(U)=>U?(U/1024/1024).toFixed(1):"N/A",_=(U)=>U?"✅":"\uD83D\uDEAB";Z.push("| Model | Size (MB) | Processing Buffer (MB) | Total Required (MB) | Fits GPU | Fits CPU |"),Z.push("|-------|-----------|------------------------|---------------------|----------|----------|"),O.forEach(({modelId:U,modelInfo:Q})=>{let K=q(Q?.modelBytes),B=q(Q?.processingBytes||Q?.kvCacheBytes),L=q(Q?.fit?.totalRequiredBytes),F=_(Q?.fit?.fitsInGpu),E=_(Q?.fit?.fitsInCpu);Z.push(`| ${U} | ${K} | ${B} | ${L} | ${F} | ${E} |`)}),Z.push(`
|
|
62
|
+
---`),Z.push(`
|
|
63
|
+
### System Information`);let G=null;if(process.platform!=="win32")try{G=K4("uname -a",{encoding:"utf8"}).trim()}catch{}if(G)Z.push(`- **System:** ${G}`);else Z.push(`- **Hostname:** ${c.hostname()}`),Z.push(`- **OS:** ${c.type()} ${c.release()}`);if(Z.push(`- **Platform:** ${process.platform}`),Z.push(`- **CPU Cores:** ${c.cpus().length}`),Z.push(`- **Total System Memory:** ${(c.totalmem()/1024/1024/1024).toFixed(2)} GB`),O.length>0){let Q=O[0].capabilities.buttress?.selected;if(Q){let K=Q.cpuTotalBytes>0?(Q.cpuUsableBytes/Q.cpuTotalBytes*100).toFixed(0):0;if(Z.push(`- **Usable CPU Memory:** ${(Q.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${K}% of ${(Q.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),Q.hasGpu){let B=Q.devices.filter((L)=>L.type==="gpu");if(B.length>0){let L=B[0];Z.push(`- **GPU Backend:** ${L.backend}`),Z.push(`- **GPU Name:** ${L.deviceName}`),Z.push(`- **GPU Total Memory:** ${(L.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let F=Q.gpuTotalBytes>0?(Q.gpuUsableBytes/Q.gpuTotalBytes*100).toFixed(0):0;Z.push(`- **GPU Usable Memory:** ${(Q.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${F}% of ${(Q.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`)}}else Z.push("- **GPU:** Not available")}}Z.push(`
|
|
64
|
+
### Command Used`);let w=process.argv.slice(2).join(" ");if(Z.push(`\`\`\`bash
|
|
65
|
+
${process.argv[0]} ${process.argv[1]} ${w}
|
|
66
|
+
\`\`\``),Z.push(`
|
|
67
|
+
### Package Information`),Z.push(`- **Name:** ${"@fugood/buttress-server"}`),Z.push(`- **Version:** ${"2.23.0-beta.53"}`),X&&Object.keys(X).length>0){Z.push(`
|
|
68
|
+
### Configuration`),Z.push("<details>"),Z.push("<summary>Click to expand TOML configuration</summary>"),Z.push("\n```toml");try{let U=P3.stringify(X);Z.push(U)}catch(U){Z.push("# Error serializing config"),Z.push(JSON.stringify(X,null,2))}Z.push("```"),Z.push("</details>")}let A=`ggml-stt-model-capabilities-${new Date().toISOString().replace(/[.:]/g,"-").split("T")[0]}.md`,R=S3.join(process.cwd(),A);x3.writeFileSync(R,Z.join(`
|
|
69
|
+
`),"utf8"),console.log(`
|
|
70
|
+
STT model capabilities table saved to: ${R}`),process.exit(0)}catch(W){console.error("Failed to generate STT model table:",W.message),process.exit(1)}}async function D3({modelId:$=null,defaultConfig:X=null}={}){if(console.log(`${"@fugood/buttress-server"} v${"2.23.0-beta.53"}`),console.log("Testing capabilities for backend: ggml-stt"),$)console.log(`Model: ${$}`);console.log("--------------------------------");try{let{repoId:Z,filename:W}=T3($),j=X||{},{server:V,generators:Y=[],...J}=j,N=(z={},A={})=>{let R=Array.isArray(z)?[...z]:{...z};return Object.entries(A||{}).forEach(([U,Q])=>{if(Q&&typeof Q==="object"&&!Array.isArray(Q))R[U]=N(R[U]||{},Q);else R[U]=Q}),R},H=(z)=>N(JSON.parse(JSON.stringify(J)),z||{}),q=((z)=>{if(Array.isArray(Y)&&Y.length>0){let A=Y.filter((R)=>R?.type==="ggml-stt");if(A.length>0){if(z){let R=A.find((U)=>U.model?.repo_id===z);if(R)return H(R)}}}if(Object.keys(J).length>0)return H({});return null})(Z);if(Z)q={...q||{},model:{...J.runtime,...q?.model||{},repo_id:Z,...W&&{filename:W}}};let _=await t("ggml-stt",null,{config:q,includeBreakdown:!0}),G=_.buttress?.selected||null,w=_.modelConfig||null;if(Z||w?.repoId){console.log(`
|
|
71
|
+
=== Model Information ===`);let z=Z||w?.repoId;if(console.log(`Repository ID: ${z}`),W)console.log(`Filename: ${W}`);if(G?.modelBytes)console.log(`Model Size: ${(G.modelBytes/1024/1024).toFixed(1)} MB`);let A=G?.processingBytes||G?.kvCacheBytes;if(A)console.log(`Processing Buffer: ${(A/1024/1024).toFixed(1)} MB`);if(G?.modelBytes&&A)console.log(`Total Required Memory: ${((G.modelBytes+A)/1024/1024).toFixed(1)} MB`);else if(_.buttress?.selected?.fit){let{totalRequiredBytes:R}=_.buttress.selected.fit;console.log(`Total Required Memory: ${(R/1024/1024).toFixed(1)} MB`)}}if(_.buttress?.selected){let{selected:z}=_.buttress;console.log(`
|
|
72
|
+
=== Hardware Information ===`);let A=null;if(process.platform!=="win32")try{A=K4("uname -a",{encoding:"utf8"}).trim()}catch{}if(A)console.log(`System: ${A}`);else console.log(`Hostname: ${c.hostname()}`),console.log(`OS: ${c.type()} ${c.release()}`);console.log(`Platform: ${z.platform}`),console.log(`CPU Cores: ${c.cpus().length}`),console.log(`Total System Memory: ${(c.totalmem()/1024/1024/1024).toFixed(2)} GB`);let R=z.cpuTotalBytes>0?(z.cpuUsableBytes/z.cpuTotalBytes*100).toFixed(0):0;if(console.log(`Usable CPU Memory: ${(z.cpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${R}% of ${(z.cpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),z.hasGpu)console.log(`
|
|
73
|
+
--- GPU Details ---`),z.devices.filter((Q)=>Q.type==="gpu").forEach((Q)=>{console.log(`GPU Backend: ${Q.backend}`),console.log(`GPU Name: ${Q.deviceName}`),console.log(`GPU Total Memory: ${(Q.maxMemorySize/1024/1024/1024).toFixed(2)} GB`);let K=z.gpuTotalBytes>0?(z.gpuUsableBytes/z.gpuTotalBytes*100).toFixed(0):0;if(console.log(`GPU Usable Memory: ${(z.gpuUsableBytes/1024/1024/1024).toFixed(2)} GB (${K}% of ${(z.gpuTotalBytes/1024/1024/1024).toFixed(2)} GB)`),Q.metadata){if(Q.metadata.hasBFloat16)console.log("Supports BFloat16: Yes");if(Q.metadata.hasUnifiedMemory)console.log("Unified Memory: Yes")}});else console.log("GPU: Not available");if(console.log(`
|
|
74
|
+
Backend Variant: ${z.variant}`),console.log(`Performance Score: ${z.score}`),z.fit)console.log(`
|
|
75
|
+
--- Model Fit Analysis ---`),console.log(`Fits in GPU: ${z.fit.fitsInGpu?"Yes":"No"}`),console.log(`Fits in CPU: ${z.fit.fitsInCpu?"Yes":"No"}`),console.log(`Limiting Factor: ${z.fit.limiting}`)}console.log(`
|
|
76
|
+
=== Full Capabilities JSON ===`),console.log(JSON.stringify(_,null,2)),process.exit(0)}catch(Z){console.error("Failed to get capabilities:",Z.message),process.exit(1)}}var T3=($)=>{if(!$)return{repoId:null,filename:null};let[X,Z]=$.split(":");return{repoId:X,filename:Z||null}};var b3=P(()=>{V4();M0()});var d9={};import y9 from"node:fs";import u9 from"node:path";import f9 from"@iarna/toml";async function g9($){if(!$?.generators||!Array.isArray($.generators))return;let X=$.generators.filter((O)=>{if(!O.model?.download)return!1;let{type:q}=O;if(!q||q!=="ggml-llm"&&q!=="ggml-stt")return console.warn(`[Download] Skipping unknown generator type: ${q}`),!1;return!0});if(X.length===0)return;let{server:Z,generators:W,...j}=$,V=X.map((O)=>{let{type:q}=O,_=O.model?.repo_id;console.log(`[Download] Starting pre-download for ${q}: ${_}`);let G={...j,backend:O.backend||{},model:O.model||{},runtime:{...j.runtime,...O.runtime||{}}};return Q4(q,G,{onProgress:()=>{},onComplete:({repoId:w,alreadyExists:z})=>{if(z)console.log(`[Download] Pre-download complete (already exists): ${w}`);else console.log(`[Download] Pre-download complete: ${w}`)},onError:(w)=>{console.error(`[Download] Pre-download failed for ${_}:`,w.message)}})}),Y=await Promise.all(V),J=Y.filter((O)=>O.started).length,N=Y.filter((O)=>O.alreadyExists).length,H=Y.filter((O)=>O.alreadyDownloading).length;console.log(`[Download] Pre-download summary: ${J} started, ${N} already exist, ${H} already downloading`)}var v3,h3,I3,q4,J0=null,p9,m9,C3,_4,c9;var y3=P(async()=>{b3();b0();q1();await g3();if(process.argv.includes("--version")||process.argv.includes("-v"))console.log("2.23.0-beta.53"),process.exit(0);if(process.argv.includes("--help")||process.argv.includes("-h"))console.log(`
|
|
77
|
+
bricks-buttress v${"2.23.0-beta.53"}
|
|
78
|
+
|
|
79
|
+
Buttress server for remote inference with GGML backends.
|
|
80
|
+
|
|
81
|
+
Usage:
|
|
82
|
+
bricks-buttress [options]
|
|
83
|
+
|
|
84
|
+
Options:
|
|
85
|
+
-h, --help Show this help message
|
|
86
|
+
-v, --version Show version number
|
|
87
|
+
-p, --port <port> Port to listen on (default: 2080)
|
|
88
|
+
-c, --config <path|toml> Path to TOML config file or inline TOML string
|
|
89
|
+
|
|
90
|
+
Testing Options:
|
|
91
|
+
--test-caps <backend> Test model capabilities (ggml-llm or ggml-stt)
|
|
92
|
+
--test-caps-model-id <id> Model ID to test (used with --test-caps)
|
|
93
|
+
--test-models <ids> Comma-separated list of model IDs to test
|
|
94
|
+
--test-models-default Test default set of models
|
|
95
|
+
|
|
96
|
+
Note: --test-models and --test-models-default output a markdown report
|
|
97
|
+
file (e.g., ggml-llm-model-capabilities-YYYY-MM-DD.md)
|
|
98
|
+
|
|
99
|
+
Environment Variables:
|
|
100
|
+
NODE_ENV Set to 'development' for dev mode
|
|
101
|
+
ENABLE_OPENAI_COMPAT_ENDPOINT Set to '1' to enable OpenAI-compatible API
|
|
102
|
+
|
|
103
|
+
Examples:
|
|
104
|
+
bricks-buttress
|
|
105
|
+
bricks-buttress --port 3000
|
|
106
|
+
bricks-buttress --config ./config.toml
|
|
107
|
+
bricks-buttress --test-caps ggml-llm --test-models-default
|
|
108
|
+
bricks-buttress --test-caps ggml-stt --test-caps-model-id BricksDisplay/whisper-ggml:ggml-small.bin
|
|
109
|
+
`),process.exit(0);v3=process.argv.findIndex(($)=>$==="--port"||$==="-p"),h3=v3>=0?Number(process.argv[v3+1]):void 0,I3=process.argv.findIndex(($)=>$==="--config"||$==="-c"),q4=I3>=0?process.argv[I3+1]:null;if(q4){let $;if(q4.includes(`
|
|
110
|
+
`))$=q4;else{let X=u9.resolve(q4);try{$=y9.readFileSync(X,"utf8")}catch(Z){console.error(`Failed to read Buttress config at ${X}:`,Z),process.exit(1)}}try{let X=f9.parse($);if(X.env&&typeof X.env==="object")Object.entries(X.env).forEach(([Z,W])=>{if(process.env[Z]===void 0)process.env[Z]=String(W)}),delete X.env;J0=X}catch(X){console.error("Failed to parse TOML config:",X),process.exit(1)}}p9=["ggml-org/gpt-oss-20b-GGUF","ggml-org/gpt-oss-120b-GGUF","unsloth/Nemotron-3-Nano-30B-A3B-GGUF","unsloth/Qwen3-30B-A3B-Instruct-2507-GGUF","unsloth/GLM-4.7-Flash-GGUF","bartowski/Mistral-Nemo-Instruct-2407-GGUF","mistralai/Magistral-Small-2509-GGUF","mistralai/Ministral-3-14B-Reasoning-2512-GGUF","bartowski/mistralai_Devstral-Small-2-24B-Instruct-2512-GGUF","bartowski/mistralai_Devstral-2-123B-Instruct-2512-GGUF","ggml-org/gemma-3-12b-it-qat-GGUF","ggml-org/gemma-3-27b-it-qat-GGUF","unsloth/phi-4-GGUF"],m9=["BricksDisplay/whisper-ggml:ggml-small.bin","BricksDisplay/whisper-ggml:ggml-small-q8_0.bin","BricksDisplay/whisper-ggml:ggml-medium.bin","BricksDisplay/whisper-ggml:ggml-medium-q8_0.bin","BricksDisplay/whisper-ggml:ggml-large-v3-turbo.bin","BricksDisplay/whisper-ggml:ggml-large-v3-turbo-q8_0.bin","BricksDisplay/whisper-ggml:ggml-large-v3.bin"],C3=process.argv.findIndex(($)=>$==="--test-caps");if(C3>=0){let $=process.argv[C3+1]||"ggml-llm";if($!=="ggml-llm"&&$!=="ggml-stt")console.error("Only ggml-llm and ggml-stt backends are supported for testing capabilities"),process.exit(1);let X=process.argv.findIndex((W)=>W==="--test-models"),Z=process.argv.includes("--test-models-default");if($==="ggml-stt")if(X>=0){let W=process.argv[X+1];if(!W)console.error("Error: --test-models requires a comma-separated list of model IDs"),process.exit(1);let j=W.split(",").map((V)=>V.trim());await L1({modelIds:j,defaultConfig:J0})}else if(Z)await L1({modelIds:m9,defaultConfig:J0});else{let W=process.argv.findIndex((V)=>V==="--test-caps-model-id"),j=W>=0?process.argv[W+1]:null;await D3({modelId:j,defaultConfig:J0})}else if(X>=0){let W=process.argv[X+1];if(!W)console.error("Error: --test-models requires a comma-separated list of model IDs"),process.exit(1);let j=W.split(",").map((V)=>V.trim());await _1({modelIds:j,defaultConfig:J0})}else if(Z)await _1({modelIds:p9,defaultConfig:J0});else{let W=process.argv.findIndex((V)=>V==="--test-caps-model-id"),j=W>=0?process.argv[W+1]:null;await k3({modelId:j,defaultConfig:J0})}}_4=G1(J0);if(h3)_4.server.port=h3;if(!_4.server.port)_4.server.port=2080;c9=process.env.ENABLE_OPENAI_COMPAT_ENDPOINT==="1";f3({config:_4,enableOpenAICompat:c9}).then(async({port:$,openaiEnabled:X,autoDiscover:Z})=>{let W=O4();if(console.log(`Buttress server listening on port ${$}`),console.log("--------------------------------"),await u3(),console.log(),console.log("Current supported Generators:"),console.log("- LLM (GGML)"),console.log("- STT (GGML)"),console.log(),console.log("Please configure `Buttress (Remote Inference)` in the Generator to connect to this server."),console.log(),console.log(`- Use http://${W}:${$} to connect to this server via LAN.`),console.log(`- Visit http://${W}:${$}/status to see status via LAN.`),console.log(),X)console.log("OpenAI-compatible API [EXPERIMENTAL]:"),console.log(`- Base URL: http://${W}:${$}/oai-compat/v1`),console.log(`- Chat completions: POST http://${W}:${$}/oai-compat/v1/chat/completions`),console.log(`- Models: GET http://${W}:${$}/oai-compat/v1/models`),console.log();else console.log("OpenAI-compatible API [EXPERIMENTAL]: disabled"),console.log(" Set ENABLE_OPENAI_COMPAT_ENDPOINT=1 to enable"),console.log();if(Z)console.log("Auto-discover enabled"),console.log();if(J0)await g9(J0)}).catch(($)=>{console.error("Failed to start Buttress server:",$),process.exitCode=1})});import{node as l9}from"@elysiajs/node";import{Elysia as i9,t as v0}from"elysia";import{ReadableStream as s9}from"node:stream/web";import n9 from"node:fs/promises";import{ZodError as r9}from"zod";var o9=async()=>{let $=`https://registry.npmjs.org/${"@fugood/buttress-server"}/latest`;try{let X=new AbortController,Z=setTimeout(()=>X.abort(),3000),W=await fetch($,{headers:{Accept:"application/json"},signal:X.signal});if(clearTimeout(Z),!W.ok)return null;return(await W.json()).version||null}catch{return null}},a9=($,X)=>{if(!X)return!1;let Z=$.split(/[.-]/),W=X.split(/[.-]/);for(let j=0;j<Math.max(Z.length,W.length);j+=1){let V=parseInt(Z[j])||0,Y=parseInt(W[j])||0;if(Y>V)return!0;if(Y<V)return!1}return!1},t9=($)=>{console.log(""),console.log("\x1B[33mâ•─────────────────────────────────────────────────╮\x1B[0m"),console.log("\x1B[33m│\x1B[0m Update available! \x1B[2m%s\x1B[0m → \x1B[32m%s\x1B[0m","2.23.0-beta.53".padEnd(12),$.padEnd(12),"\x1B[33m│\x1B[0m"),console.log("\x1B[33m│\x1B[0m \x1B[33m│\x1B[0m"),console.log("\x1B[33m│\x1B[0m Run to upgrade: \x1B[33m│\x1B[0m"),console.log("\x1B[33m│\x1B[0m \x1B[36mnpm install -g %s\x1B[0m \x1B[33m│\x1B[0m","@fugood/buttress-server".padEnd(27)),console.log("\x1B[33m╰─────────────────────────────────────────────────╯\x1B[0m"),console.log("")},u3=async()=>{try{let $=await o9();if($&&a9("2.23.0-beta.53",$))t9($)}catch($){}},e9,$$=async({backend:$,router:X,config:Z,enableOpenAICompat:W})=>{try{await n9.mkdir(Z.server.temp_file_dir,{recursive:!0})}catch{}let j=O4()||"0.0.0.0",V={id:Z.server.id,name:Z.server.name,version:"2.23.0-beta.53",address:j,port:Z.server.port,url:`http://${j}:${Z.server.port}`,generators:V3(Z,Z.generators.map((N)=>N.type)),authentication:{required:!0,type:"device-group"}},Y=new i9({serve:{maxRequestBodySize:Z.server.max_body_size},websocket:{idleTimeout:Math.ceil(Z.server.session_timeout/1000)},adapter:e9?l9():void 0}).state({sessions:new Map,backend:$||J4,config:Z,serverInfo:V});if(X)Y.use(X);if(Z.autodiscover?.http?.enabled)Y.use(j1(Z));if(Y.use(Q1),Y.use(V1),W)Y.use(U4(Z));let J={INVALID_REQUEST:-32600,INVALID_PARAMS:-32602,METHOD_NOT_FOUND:-32601,INTERNAL_ERROR:-32603};return Y.ws("/buttress/rpc",{parse:(N,H)=>{if(typeof H==="string")try{return JSON.parse(H)}catch{return N.send(JSON.stringify({jsonrpc:"2.0",error:{code:J.INVALID_REQUEST,message:"Invalid request"},id:null})),null}return H},body:v0.Object({jsonrpc:v0.String(),method:v0.String(),params:v0.String(),id:v0.String()}),open(N){let H=N.id??N.raw?.id??N.remoteAddress;if(console.log(`[Request] New connection: ${H}`),!N.data.store.sessions.has(H))N.data.store.sessions.set(H,{streamReaders:new Map,generators:new Set,initializedContexts:new Set,timeout:null});else{let O=N.data.store.sessions.get(H);clearTimeout(O.timeout),O.timeout=null}},async message(N,{id:H,method:O,params:q}){let _=N.id??N.raw?.id??N.remoteAddress;console.log(`[Request] Received request from ${_}: ${O}`);let G=N.data.store.sessions.get(_),[w,z]=O.split("."),A=z3[w]?.[z];if(!A){N.send(JSON.stringify({jsonrpc:"2.0",error:{code:J.METHOD_NOT_FOUND,message:"Method not found"},id:H}));return}try{if(O==="cancel"){if(G.streamReaders.has(H))G.streamReaders.get(H)?.cancel(),G.streamReaders.delete(H);return}if(O==="ping"){N.send(JSON.stringify({jsonrpc:"2.0",result:"pong",id:H}));return}let R=R3(q),U=A3[w]?.[z],Q=U?U.parse(R):R,K={...N.data.store,peerId:_,session:G},B=await A(K,...Q);if(B instanceof s9){let L=B.getReader();G.streamReaders.set(H,L),N.send(JSON.stringify({jsonrpc:"2.0",result:{type:"stream"},id:H}));try{while(!0){let{value:F,done:E}=await L.read();if(E)break;let{event:M,data:x}=F;N.send(JSON.stringify({jsonrpc:"2.0",method:`notification/${M}`,params:H4(x),id:H}))}N.send(JSON.stringify({jsonrpc:"2.0",method:"notification/_end",id:H}))}catch(F){console.error(F),N.send(JSON.stringify({jsonrpc:"2.0",method:"notification/_error",params:H4(F),id:H}))}G.streamReaders.delete(H)}else N.send(JSON.stringify({jsonrpc:"2.0",result:H4(B),id:H}))}catch(R){if(R instanceof r9){N.send(JSON.stringify({jsonrpc:"2.0",error:{code:J.INVALID_PARAMS,message:"Invalid params",data:R.issues},id:H}));return}console.error(R),N.send(JSON.stringify({jsonrpc:"2.0",error:{code:J.INTERNAL_ERROR,message:String(R)},id:H}))}},async close(N){let H=N.id??N.raw?.id??N.remoteAddress;console.log(`[Request] Connection closed: ${H}`);let{backend:O,sessions:q}=N.data.store,_=q.get(H);if(!_)return;_.streamReaders.forEach((G)=>G.cancel()),_.streamReaders.clear(),_.timeout=setTimeout(()=>{q.delete(H),console.log(`[Request] Session timed out: ${H}`);let{generators:G}=_;G.forEach((w)=>{O.finalizeGenerator(w)})},Z.server.session_timeout)}}),{app:Y,config:Z}},f3=async({backend:$,router:X,config:Z,enableOpenAICompat:W=!1})=>{let{app:j,config:V}=await $$({backend:$,router:X,config:Z,enableOpenAICompat:W}),{server:{port:Y}}=V,J=[new Promise((H)=>j.listen(Y,H))],N=null;if(V.autodiscover)N=new K1(V.autodiscover,()=>j.store.serverInfo),J.push(N.start());return await Promise.all(J),{app:j,port:Y,openaiEnabled:W,autoDiscover:N}};var g3=P(async()=>{V4();Z3();B3();w3();b0();M3();q1();V4();b0();e9=typeof process<"u"&&process.versions&&process.versions.node;if(R1.main==R1.module)await y3().then(() => d9)});await g3();export{f3 as startServer,Q4 as startModelDownload,G1 as processConfig,t9 as logUpdateMessage,$$ as createServer,a9 as compareVersions,o9 as checkForUpdates,u3 as checkAndNotifyUpdates};
|