Spaces:
Configuration error
Configuration error
Deploy prebuilt static pi web agent
Browse files- .gitignore +9 -0
- README.md +115 -6
- dist/assets/anthropic-CoMwX3KG.js +0 -0
- dist/assets/azure-openai-responses-C18lMu0S.js +1 -0
- dist/assets/client-Sq9UPDIz.js +0 -0
- dist/assets/github-copilot-headers-CVWOSrQr.js +1 -0
- dist/assets/google-CebhgvzR.js +1 -0
- dist/assets/google-shared-CZIKa0Kf.js +0 -0
- dist/assets/google-vertex-BFhNAm5u.js +1 -0
- dist/assets/hash-DMDecQcg.js +1 -0
- dist/assets/headers-CgnjaPPL.js +1 -0
- dist/assets/index-BCicI0Zo.js +0 -0
- dist/assets/index-Brjf2SFV.js +2 -0
- dist/assets/index-CRS_ZGq-.css +1 -0
- dist/assets/json-parse-B9IFJnx2.js +2 -0
- dist/assets/mistral-DpUPGD8Y.js +0 -0
- dist/assets/openai-codex-responses-j10R-njq.js +7 -0
- dist/assets/openai-completions-DroT7VTp.js +6 -0
- dist/assets/openai-prompt-cache-h_V5PL7P.js +1 -0
- dist/assets/openai-responses-C3CNUo8J.js +1 -0
- dist/assets/openai-responses-shared-DeOhQuRA.js +12 -0
- dist/assets/openrouter-C9vLcqev.js +1 -0
- dist/assets/ort-wasm-simd-threaded.asyncify-DMmc6YqF.wasm +3 -0
- dist/assets/sanitize-unicode-B62XMGgN.js +1 -0
- dist/assets/transform-messages-Bxe2XUr4.js +1 -0
- dist/index.html +82 -0
- index.html +79 -17
- package-lock.json +0 -0
- package.json +23 -0
- scripts/convert_minicpm5_tjs.py +229 -0
- scripts/smoke_local_model_web_agent.mjs +61 -0
- scripts/smoke_web_agent.mjs +60 -0
- scripts/verify_tjs_model.mjs +31 -0
- src/main.js +192 -0
- src/piAgent.js +348 -0
- src/sandbox.js +183 -0
- src/styles.css +225 -0
- vite.config.js +16 -0
.gitignore
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
node_modules/
|
| 2 |
+
dist/
|
| 3 |
+
.vite/
|
| 4 |
+
.cache/
|
| 5 |
+
output/
|
| 6 |
+
refs/
|
| 7 |
+
source/
|
| 8 |
+
.swapfile
|
| 9 |
+
*.log
|
README.md
CHANGED
|
@@ -1,10 +1,119 @@
|
|
| 1 |
---
|
| 2 |
-
title: MiniCPM5 Pi Web Agent
|
| 3 |
-
emoji: 📈
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: green
|
| 6 |
sdk: static
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MiniCPM5 Pi Web Agent
|
|
|
|
|
|
|
|
|
|
| 3 |
sdk: static
|
| 4 |
+
app_file: dist/index.html
|
| 5 |
+
fullWidth: true
|
| 6 |
+
models:
|
| 7 |
+
- Mike0021/MiniCPM5-1B-ONNX-Web
|
| 8 |
+
custom_headers:
|
| 9 |
+
cross-origin-embedder-policy: credentialless
|
| 10 |
+
cross-origin-opener-policy: same-origin
|
| 11 |
+
cross-origin-resource-policy: cross-origin
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# MiniCPM5-1B Pi Web Agent
|
| 15 |
+
|
| 16 |
+
This workspace converts `openbmb/MiniCPM5-1B` into a browser-loadable Transformers.js model and ships a browser-only pi agent app.
|
| 17 |
+
|
| 18 |
+
Published artifact: https://huggingface.co/Mike0021/MiniCPM5-1B-ONNX-Web
|
| 19 |
+
|
| 20 |
+
The required runtime layout is:
|
| 21 |
+
|
| 22 |
+
- `config.json`, `generation_config.json`, tokenizer files, and `chat_template.jinja` at the repo root
|
| 23 |
+
- q4 ONNX weights at `onnx/model_q4.onnx`
|
| 24 |
+
- `config.json` includes `transformers.js_config.dtype = "q4"` so the default loader selects the web-sized artifact
|
| 25 |
+
|
| 26 |
+
The conversion uses an ONNX export with KV cache (`text-generation-with-past`) and then applies ONNX Runtime 4-bit MatMul quantization. A generic ONNX export without KV cache is not enough for normal Transformers.js autoregressive generation.
|
| 27 |
+
|
| 28 |
+
## Run the Web App
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
npm install
|
| 32 |
+
npm run dev
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
Open http://localhost:5173/.
|
| 36 |
+
|
| 37 |
+
The app uses:
|
| 38 |
+
|
| 39 |
+
- `@earendil-works/pi-agent-core` for the agent loop, transcript state, and tool execution.
|
| 40 |
+
- `@huggingface/transformers` with `Mike0021/MiniCPM5-1B-ONNX-Web` for the local browser model.
|
| 41 |
+
- `@webcontainer/api` for the client-only sandbox with a virtual filesystem and browser-contained Node.js processes.
|
| 42 |
+
|
| 43 |
+
Vite serves the app with COOP/COEP headers and boots WebContainers with `coep: "credentialless"`. The deterministic test model is available at `http://localhost:5173/?mode=mock&device=wasm` for fast harness and sandbox smoke tests without downloading the full ONNX model.
|
| 44 |
+
|
| 45 |
+
The Static Space uses the same isolation policy through `custom_headers` in this README frontmatter. The app is built with `npm run build` and the generated `dist/` directory is uploaded to the Space.
|
| 46 |
+
|
| 47 |
+
## Test the Agent App
|
| 48 |
+
|
| 49 |
+
Start the dev server, then run:
|
| 50 |
+
|
| 51 |
+
```bash
|
| 52 |
+
npm run smoke:web
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
The smoke test opens Chromium, confirms `crossOriginIsolated`, boots the WebContainer sandbox, runs the pi agent in deterministic mode, writes `hello.js`, spawns `node hello.js`, and checks for `pi sandbox result: 42` in the transcript.
|
| 56 |
+
|
| 57 |
+
For the heavier end-to-end check with the real MiniCPM5 ONNX model in browser WASM mode:
|
| 58 |
+
|
| 59 |
+
```bash
|
| 60 |
+
npm run smoke:local-model
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
This downloads/loads the q4 ONNX artifact in Chromium, runs the same pi/WebContainer task, and checks that the model reaches `Model ready` before the sandbox result is accepted.
|
| 64 |
+
|
| 65 |
+
## Verify the Published Artifact
|
| 66 |
+
|
| 67 |
+
```bash
|
| 68 |
+
npm install
|
| 69 |
+
node scripts/verify_tjs_model.mjs Mike0021/MiniCPM5-1B-ONNX-Web
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
The verifier asks Transformers.js for the `text-generation` file plan, checks for `onnx/model_q4.onnx`, then loads the model and generates a short completion.
|
| 73 |
+
|
| 74 |
+
## Convert and Upload
|
| 75 |
+
|
| 76 |
+
The published repo was produced locally with a CPU fp16 export followed by q4 ONNX quantization:
|
| 77 |
+
|
| 78 |
+
```bash
|
| 79 |
+
uv run --python 3.12 \
|
| 80 |
+
--with "numpy<2" \
|
| 81 |
+
--with "transformers==4.57.6" \
|
| 82 |
+
--with "optimum[onnx]" \
|
| 83 |
+
--with "onnxruntime==1.20.1" \
|
| 84 |
+
--with onnxslim \
|
| 85 |
+
--with "huggingface_hub>=0.33" \
|
| 86 |
+
--with accelerate \
|
| 87 |
+
--with sentencepiece \
|
| 88 |
+
--with protobuf \
|
| 89 |
+
scripts/convert_minicpm5_tjs.py \
|
| 90 |
+
--source-model openbmb/MiniCPM5-1B \
|
| 91 |
+
--target-repo Mike0021/MiniCPM5-1B-ONNX-Web \
|
| 92 |
+
--output-dir output/MiniCPM5-1B-ONNX-Web \
|
| 93 |
+
--work-dir output/minicpm5-work \
|
| 94 |
+
--device cpu \
|
| 95 |
+
--export-dtype fp16
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
For a clean remote conversion, the same script can be run on Hugging Face Jobs with a configured Hub token:
|
| 99 |
+
|
| 100 |
+
```bash
|
| 101 |
+
hf repos create Mike0021/MiniCPM5-1B-ONNX-Web --repo-type model --exist-ok
|
| 102 |
+
hf jobs uv run scripts/convert_minicpm5_tjs.py \
|
| 103 |
+
--flavor l4x1 \
|
| 104 |
+
--timeout 6h \
|
| 105 |
+
--secrets HF_TOKEN \
|
| 106 |
+
--with "numpy<2" \
|
| 107 |
+
--with "transformers==4.57.6" \
|
| 108 |
+
--with "optimum[onnx]" \
|
| 109 |
+
--with "onnxruntime==1.20.1" \
|
| 110 |
+
--with onnxslim \
|
| 111 |
+
--with "huggingface_hub>=0.33" \
|
| 112 |
+
--with accelerate \
|
| 113 |
+
--with sentencepiece \
|
| 114 |
+
--with protobuf \
|
| 115 |
+
--python 3.12 \
|
| 116 |
+
-- \
|
| 117 |
+
--target-repo Mike0021/MiniCPM5-1B-ONNX-Web \
|
| 118 |
+
--export-dtype fp16
|
| 119 |
+
```
|
dist/assets/anthropic-CoMwX3KG.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dist/assets/azure-openai-responses-C18lMu0S.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
import{O as h,a as c,r as m,i as _,b as R}from"./client-Sq9UPDIz.js";import{A as y,g as l,a as I}from"./index-BCicI0Zo.js";import{h as g}from"./headers-CgnjaPPL.js";import{c as v}from"./openai-prompt-cache-h_V5PL7P.js";import{p as O,c as N,a as b}from"./openai-responses-shared-DeOhQuRA.js";import{b as P}from"./transform-messages-Bxe2XUr4.js";import"./hash-DMDecQcg.js";import"./json-parse-B9IFJnx2.js";import"./sanitize-unicode-B62XMGgN.js";var w={};class U extends h{constructor({baseURL:t=m("OPENAI_BASE_URL"),apiKey:e=m("AZURE_OPENAI_API_KEY"),apiVersion:s=m("OPENAI_API_VERSION"),endpoint:n,deployment:a,azureADTokenProvider:o,dangerouslyAllowBrowser:i,...u}={}){if(!s)throw new c("The OPENAI_API_VERSION environment variable is missing or empty; either provide it, or instantiate the AzureOpenAI client with an apiVersion option, like new AzureOpenAI({ apiVersion: 'My API Version' }).");if(typeof o=="function"&&(i=!0),!o&&!e)throw new c("Missing credentials. Please pass one of `apiKey` and `azureADTokenProvider`, or set the `AZURE_OPENAI_API_KEY` environment variable.");if(o&&e)throw new c("The `apiKey` and `azureADTokenProvider` arguments are mutually exclusive; only one can be passed at a time.");if(u.defaultQuery={...u.defaultQuery,"api-version":s},t){if(n)throw new c("baseURL and endpoint are mutually exclusive")}else{if(n||(n=w.AZURE_OPENAI_ENDPOINT),!n)throw new c("Must provide one of the `baseURL` or `endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable");t=`${n}/openai`}super({apiKey:o??e,baseURL:t,...u,...i!==void 0?{dangerouslyAllowBrowser:i}:{}}),this.apiVersion="",this.apiVersion=s,this.deploymentName=a}async buildRequest(t,e={}){if(z.has(t.path)&&t.method==="post"&&t.body!==void 0){if(!_(t.body))throw new Error("Expected request body to be an object");const s=this.deploymentName||t.body.model||t.__metadata?.model;s!==void 0&&!this.baseURL.includes("/deployments")&&(t.path=`/deployments/${s}${t.path}`)}return super.buildRequest(t,e)}async authHeaders(t){return typeof this._options.apiKey=="string"?R([{"api-key":this.apiKey}]):super.authHeaders(t)}}const z=new Set(["/completions","/chat/completions","/embeddings","/audio/transcriptions","/audio/translations","/audio/speech","/images/generations","/batches","/images/edits"]);var p={};const S="v1",M=new Set(["openai","openai-codex","opencode","azure-openai-responses"]);function L(r){const t=new Map;if(!r)return t;for(const e of r.split(",")){const s=e.trim();if(!s)continue;const[n,a]=s.split("=",2);!n||!a||t.set(n.trim(),a.trim())}return t}function Z(r,t){return t?.azureDeploymentName?t.azureDeploymentName:L(p.AZURE_OPENAI_DEPLOYMENT_NAME_MAP).get(r.id)||r.id}function k(r){if(r instanceof Error){const t=r.status,e=typeof t=="number"?t:void 0;return e!==void 0?`Azure OpenAI API error (${e}): ${r.message}`:r.message}try{return JSON.stringify(r)}catch{return String(r)}}const D=(r,t,e)=>{const s=new y;return(async()=>{const n=Z(r,e),a={role:"assistant",content:[],api:"azure-openai-responses",provider:r.provider,model:r.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const o=e?.apiKey||l(r.provider)||"",i=x(r,o,e);let u=V(r,t,e,n);const d=await e?.onPayload?.(u,r);d!==void 0&&(u=d);const A={...e?.signal?{signal:e.signal}:{},...e?.timeoutMs!==void 0?{timeout:e.timeoutMs}:{},...e?.maxRetries!==void 0?{maxRetries:e.maxRetries}:{}},{data:E,response:f}=await i.responses.create(u,A).withResponse();if(await e?.onResponse?.({status:f.status,headers:g(f.headers)},r),s.push({type:"start",partial:a}),await O(E,a,s,r),e?.signal?.aborted)throw new Error("Request was aborted");if(a.stopReason==="aborted"||a.stopReason==="error")throw new Error("An unknown error occurred");s.push({type:"done",reason:a.stopReason,message:a}),s.end()}catch(o){for(const i of a.content)delete i.index,delete i.partialJson;a.stopReason=e?.signal?.aborted?"aborted":"error",a.errorMessage=k(o),s.push({type:"error",reason:a.stopReason,error:a}),s.end()}})(),s},F=(r,t,e)=>{const s=e?.apiKey||l(r.provider);if(!s)throw new Error(`No API key for provider: ${r.provider}`);const n=P(r,e,s),a=e?.reasoning?I(r,e.reasoning):void 0;return D(r,t,{...n,reasoningEffort:a==="off"?void 0:a})};function K(r){const t=r.trim().replace(/\/+$/,"");let e;try{e=new URL(t)}catch{throw new Error(`Invalid Azure OpenAI base URL: ${r}`)}const s=e.hostname.endsWith(".openai.azure.com")||e.hostname.endsWith(".cognitiveservices.azure.com"),n=e.pathname.replace(/\/+$/,"");return s&&(n===""||n==="/"||n==="/openai")&&(e.pathname="/openai/v1",e.search=""),e.toString().replace(/\/+$/,"")}function T(r){return`https://${r}.openai.azure.com/openai/v1`}function $(r,t){const e=t?.azureApiVersion||p.AZURE_OPENAI_API_VERSION||S,s=t?.azureBaseUrl?.trim()||p.AZURE_OPENAI_BASE_URL?.trim()||void 0,n=t?.azureResourceName||p.AZURE_OPENAI_RESOURCE_NAME;let a=s;if(!a&&n&&(a=T(n)),!a&&r.baseUrl&&(a=r.baseUrl),!a)throw new Error("Azure OpenAI base URL is required. Set AZURE_OPENAI_BASE_URL or AZURE_OPENAI_RESOURCE_NAME, or pass azureBaseUrl, azureResourceName, or model.baseUrl.");return{baseUrl:K(a),apiVersion:e}}function x(r,t,e){if(!t){if(!p.AZURE_OPENAI_API_KEY)throw new Error("Azure OpenAI API key is required. Set AZURE_OPENAI_API_KEY environment variable or pass it as an argument.");t=p.AZURE_OPENAI_API_KEY}const s={...r.headers};e?.headers&&Object.assign(s,e.headers);const{baseUrl:n,apiVersion:a}=$(r,e);return new U({apiKey:t,apiVersion:a,dangerouslyAllowBrowser:!0,defaultHeaders:s,baseURL:n})}function V(r,t,e,s){const n=N(r,t,M),a={model:s,input:n,stream:!0,prompt_cache_key:v(e?.sessionId)};if(e?.maxTokens&&(a.max_output_tokens=e?.maxTokens),e?.temperature!==void 0&&(a.temperature=e?.temperature),t.tools&&t.tools.length>0&&(a.tools=b(t.tools)),r.reasoning)if(e?.reasoningEffort||e?.reasoningSummary){const o=e?.reasoningEffort?r.thinkingLevelMap?.[e.reasoningEffort]??e.reasoningEffort:"medium";a.reasoning={effort:o,summary:e?.reasoningSummary||"auto"},a.include=["reasoning.encrypted_content"]}else r.thinkingLevelMap?.off!==null&&(a.reasoning={effort:r.thinkingLevelMap?.off??"none"});return a}export{D as streamAzureOpenAIResponses,F as streamSimpleAzureOpenAIResponses};
|
dist/assets/client-Sq9UPDIz.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dist/assets/github-copilot-headers-CVWOSrQr.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
var i={};function u(r){return r==="cloudflare-workers-ai"||r==="cloudflare-ai-gateway"}function l(r){const e=r.baseUrl;return e.includes("{")?e.replace(/\{([A-Z_][A-Z0-9_]*)\}/g,(s,n)=>{const o=i[n];if(!o)throw new Error(`${n} is required for provider ${r.provider} but is not set.`);return o}):e}function a(r){const e=r[r.length-1];return e&&e.role!=="user"?"agent":"user"}function c(r){return r.some(e=>e.role==="user"&&Array.isArray(e.content)?e.content.some(t=>t.type==="image"):e.role==="toolResult"&&Array.isArray(e.content)?e.content.some(t=>t.type==="image"):!1)}function f(r){const e={"X-Initiator":a(r.messages),"Openai-Intent":"conversation-edits"};return r.hasImages&&(e["Copilot-Vision-Request"]="true"),e}export{f as b,c as h,u as i,l as r};
|
dist/assets/google-CebhgvzR.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
import{i as T,r as b,m as v,G as S,c as R,a as L,b as _}from"./google-shared-CZIKa0Kf.js";import{A as G,g as x,c as O,a as A}from"./index-BCicI0Zo.js";import{s as N}from"./sanitize-unicode-B62XMGgN.js";import{b as E}from"./transform-messages-Bxe2XUr4.js";let P=0;const f=(e,o,n)=>{const i=new G;return(async()=>{const t={role:"assistant",content:[],api:"google-generative-ai",provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const r=n?.apiKey||x(e.provider)||"",c=W(e,r,n?.headers);let u=B(e,o,n);const y=await n?.onPayload?.(u,e);y!==void 0&&(u=y);const M=await c.models.generateContentStream(u);i.push({type:"start",partial:t});let a=null;const C=t.content,l=()=>C.length-1;for await(const g of M){t.responseId||=g.responseId;const d=g.candidates?.[0];if(d?.content?.parts)for(const s of d.content.parts){if(s.text!==void 0){const h=T(s);(!a||h&&a.type!=="thinking"||!h&&a.type!=="text")&&(a&&(a.type==="text"?i.push({type:"text_end",contentIndex:C.length-1,content:a.text,partial:t}):i.push({type:"thinking_end",contentIndex:l(),content:a.thinking,partial:t})),h?(a={type:"thinking",thinking:"",thinkingSignature:void 0},t.content.push(a),i.push({type:"thinking_start",contentIndex:l(),partial:t})):(a={type:"text",text:""},t.content.push(a),i.push({type:"text_start",contentIndex:l(),partial:t}))),a.type==="thinking"?(a.thinking+=s.text,a.thinkingSignature=b(a.thinkingSignature,s.thoughtSignature),i.push({type:"thinking_delta",contentIndex:l(),delta:s.text,partial:t})):(a.text+=s.text,a.textSignature=b(a.textSignature,s.thoughtSignature),i.push({type:"text_delta",contentIndex:l(),delta:s.text,partial:t}))}if(s.functionCall){a&&(a.type==="text"?i.push({type:"text_end",contentIndex:l(),content:a.text,partial:t}):i.push({type:"thinking_end",contentIndex:l(),content:a.thinking,partial:t}),a=null);const h=s.functionCall.id,p={type:"toolCall",id:!h||t.content.some(I=>I.type==="toolCall"&&I.id===h)?`${s.functionCall.name}_${Date.now()}_${++P}`:h,name:s.functionCall.name||"",arguments:s.functionCall.args??{},...s.thoughtSignature&&{thoughtSignature:s.thoughtSignature}};t.content.push(p),i.push({type:"toolcall_start",contentIndex:l(),partial:t}),i.push({type:"toolcall_delta",contentIndex:l(),delta:JSON.stringify(p.arguments),partial:t}),i.push({type:"toolcall_end",contentIndex:l(),toolCall:p,partial:t})}}d?.finishReason&&(t.stopReason=v(d.finishReason),t.content.some(s=>s.type==="toolCall")&&(t.stopReason="toolUse")),g.usageMetadata&&(t.usage={input:(g.usageMetadata.promptTokenCount||0)-(g.usageMetadata.cachedContentTokenCount||0),output:(g.usageMetadata.candidatesTokenCount||0)+(g.usageMetadata.thoughtsTokenCount||0),cacheRead:g.usageMetadata.cachedContentTokenCount||0,cacheWrite:0,totalTokens:g.usageMetadata.totalTokenCount||0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},O(e,t.usage))}if(a&&(a.type==="text"?i.push({type:"text_end",contentIndex:l(),content:a.text,partial:t}):i.push({type:"thinking_end",contentIndex:l(),content:a.thinking,partial:t})),n?.signal?.aborted)throw new Error("Request was aborted");if(t.stopReason==="aborted"||t.stopReason==="error")throw new Error("An unknown error occurred");i.push({type:"done",reason:t.stopReason,message:t}),i.end()}catch(r){for(const c of t.content)"index"in c&&delete c.index;t.stopReason=n?.signal?.aborted?"aborted":"error",t.errorMessage=r instanceof Error?r.message:JSON.stringify(r),i.push({type:"error",reason:t.stopReason,error:t}),i.end()}})(),i},F=(e,o,n)=>{const i=n?.apiKey||x(e.provider);if(!i)throw new Error(`No API key for provider: ${e.provider}`);const t=E(e,n,i);if(!n?.reasoning)return f(e,o,{...t,thinking:{enabled:!1}});const r=A(e,n.reasoning),c=r==="off"?"high":r,u=e;return m(u)||w(u)||k(u)?f(e,o,{...t,thinking:{enabled:!0,level:K(c,u)}}):f(e,o,{...t,thinking:{enabled:!0,budgetTokens:U(u,c,n.thinkingBudgets)}})};function W(e,o,n){const i={};return e.baseUrl&&(i.baseUrl=e.baseUrl,i.apiVersion=""),(e.headers||n)&&(i.headers={...e.headers,...n}),new S({apiKey:o,httpOptions:Object.keys(i).length>0?i:void 0})}function B(e,o,n={}){const i=R(e,o),t={};n.temperature!==void 0&&(t.temperature=n.temperature),n.maxTokens!==void 0&&(t.maxOutputTokens=n.maxTokens);const r={...Object.keys(t).length>0&&t,...o.systemPrompt&&{systemInstruction:N(o.systemPrompt)},...o.tools&&o.tools.length>0&&{tools:L(o.tools)}};if(o.tools&&o.tools.length>0&&n.toolChoice?r.toolConfig={functionCallingConfig:{mode:_(n.toolChoice)}}:r.toolConfig=void 0,n.thinking?.enabled&&e.reasoning){const u={includeThoughts:!0};n.thinking.level!==void 0?u.thinkingLevel=n.thinking.level:n.thinking.budgetTokens!==void 0&&(u.thinkingBudget=n.thinking.budgetTokens),r.thinkingConfig=u}else e.reasoning&&n.thinking&&!n.thinking.enabled&&(r.thinkingConfig=H(e));if(n.signal){if(n.signal.aborted)throw new Error("Request aborted");r.abortSignal=n.signal}return{model:e.id,contents:i,config:r}}function k(e){return/gemma-?4/.test(e.id.toLowerCase())}function m(e){return/gemini-3(?:\.\d+)?-pro/.test(e.id.toLowerCase())}function w(e){return/gemini-3(?:\.\d+)?-flash/.test(e.id.toLowerCase())}function H(e){return m(e)?{thinkingLevel:"LOW"}:w(e)?{thinkingLevel:"MINIMAL"}:k(e)?{thinkingLevel:"MINIMAL"}:{thinkingBudget:0}}function K(e,o){if(m(o))switch(e){case"minimal":case"low":return"LOW";case"medium":case"high":return"HIGH"}if(k(o))switch(e){case"minimal":case"low":return"MINIMAL";case"medium":case"high":return"HIGH"}switch(e){case"minimal":return"MINIMAL";case"low":return"LOW";case"medium":return"MEDIUM";case"high":return"HIGH"}}function U(e,o,n){return n?.[o]!==void 0?n[o]:e.id.includes("2.5-pro")?{minimal:128,low:2048,medium:8192,high:32768}[o]:e.id.includes("2.5-flash-lite")?{minimal:512,low:2048,medium:8192,high:24576}[o]:e.id.includes("2.5-flash")?{minimal:128,low:2048,medium:8192,high:24576}[o]:-1}export{f as streamGoogle,F as streamSimpleGoogle};
|
dist/assets/google-shared-CZIKa0Kf.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dist/assets/google-vertex-BFhNAm5u.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
import{i as T,r as x,m as w,G as L,c as R,a as M,b as G,T as d,R as S}from"./google-shared-CZIKa0Kf.js";import{A,c as N,a as P}from"./index-BCicI0Zo.js";import{s as U}from"./sanitize-unicode-B62XMGgN.js";import{b as D}from"./transform-messages-Bxe2XUr4.js";var f={};const v="v1",V="gcp-vertex-credentials",H={THINKING_LEVEL_UNSPECIFIED:d.THINKING_LEVEL_UNSPECIFIED,MINIMAL:d.MINIMAL,LOW:d.LOW,MEDIUM:d.MEDIUM,HIGH:d.HIGH};let K=0;const k=(t,e,n)=>{const a=new A;return(async()=>{const i={role:"assistant",content:[],api:"google-vertex",provider:t.provider,model:t.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const s=$(n),l=s?j(t,s,n?.headers):W(t,F(n),z(n),n?.headers);let g=X(t,e,n);const I=await n?.onPayload?.(g,t);I!==void 0&&(g=I);const E=await l.models.generateContentStream(g);a.push({type:"start",partial:i});let o=null;const y=i.content,c=()=>y.length-1;for await(const u of E){i.responseId||=u.responseId;const p=u.candidates?.[0];if(p?.content?.parts)for(const r of p.content.parts){if(r.text!==void 0){const h=T(r);(!o||h&&o.type!=="thinking"||!h&&o.type!=="text")&&(o&&(o.type==="text"?a.push({type:"text_end",contentIndex:y.length-1,content:o.text,partial:i}):a.push({type:"thinking_end",contentIndex:c(),content:o.thinking,partial:i})),h?(o={type:"thinking",thinking:"",thinkingSignature:void 0},i.content.push(o),a.push({type:"thinking_start",contentIndex:c(),partial:i})):(o={type:"text",text:""},i.content.push(o),a.push({type:"text_start",contentIndex:c(),partial:i}))),o.type==="thinking"?(o.thinking+=r.text,o.thinkingSignature=x(o.thinkingSignature,r.thoughtSignature),a.push({type:"thinking_delta",contentIndex:c(),delta:r.text,partial:i})):(o.text+=r.text,o.textSignature=x(o.textSignature,r.thoughtSignature),a.push({type:"text_delta",contentIndex:c(),delta:r.text,partial:i}))}if(r.functionCall){o&&(o.type==="text"?a.push({type:"text_end",contentIndex:c(),content:o.text,partial:i}):a.push({type:"thinking_end",contentIndex:c(),content:o.thinking,partial:i}),o=null);const h=r.functionCall.id,m={type:"toolCall",id:!h||i.content.some(O=>O.type==="toolCall"&&O.id===h)?`${r.functionCall.name}_${Date.now()}_${++K}`:h,name:r.functionCall.name||"",arguments:r.functionCall.args??{},...r.thoughtSignature&&{thoughtSignature:r.thoughtSignature}};i.content.push(m),a.push({type:"toolcall_start",contentIndex:c(),partial:i}),a.push({type:"toolcall_delta",contentIndex:c(),delta:JSON.stringify(m.arguments),partial:i}),a.push({type:"toolcall_end",contentIndex:c(),toolCall:m,partial:i})}}p?.finishReason&&(i.stopReason=w(p.finishReason),i.content.some(r=>r.type==="toolCall")&&(i.stopReason="toolUse")),u.usageMetadata&&(i.usage={input:(u.usageMetadata.promptTokenCount||0)-(u.usageMetadata.cachedContentTokenCount||0),output:(u.usageMetadata.candidatesTokenCount||0)+(u.usageMetadata.thoughtsTokenCount||0),cacheRead:u.usageMetadata.cachedContentTokenCount||0,cacheWrite:0,totalTokens:u.usageMetadata.totalTokenCount||0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},N(t,i.usage))}if(o&&(o.type==="text"?a.push({type:"text_end",contentIndex:c(),content:o.text,partial:i}):a.push({type:"thinking_end",contentIndex:c(),content:o.thinking,partial:i})),n?.signal?.aborted)throw new Error("Request was aborted");if(i.stopReason==="aborted"||i.stopReason==="error")throw new Error("An unknown error occurred");a.push({type:"done",reason:i.stopReason,message:i}),a.end()}catch(s){for(const l of i.content)"index"in l&&delete l.index;i.stopReason=n?.signal?.aborted?"aborted":"error",i.errorMessage=s instanceof Error?s.message:JSON.stringify(s),a.push({type:"error",reason:i.stopReason,error:i}),a.end()}})(),a},rt=(t,e,n)=>{const a=D(t,n,void 0);if(!n?.reasoning)return k(t,e,{...a,thinking:{enabled:!1}});const i=P(t,n.reasoning),s=i==="off"?"high":i,l=t;return C(l)||b(l)?k(t,e,{...a,thinking:{enabled:!0,level:Q(s,l)}}):k(t,e,{...a,thinking:{enabled:!0,budgetTokens:Z(l,s,n.thinkingBudgets)}})};function W(t,e,n,a){return new L({vertexai:!0,project:e,location:n,apiVersion:v,httpOptions:_(t,a)})}function j(t,e,n){return new L({vertexai:!0,apiKey:e,apiVersion:v,httpOptions:_(t,n)})}function _(t,e){const n={},a=B(t.baseUrl);return a&&(n.baseUrl=a,n.baseUrlResourceScope=S.COLLECTION,J(a)&&(n.apiVersion="")),(t.headers||e)&&(n.headers={...t.headers,...e}),Object.keys(n).length>0?n:void 0}function B(t){const e=t.trim();if(!(!e||e.includes("{location}")))return e}function J(t){try{return new URL(t).pathname.split("/").some(n=>/^v\d+(?:beta\d*)?$/.test(n))}catch{return/(?:^|\/)v\d+(?:beta\d*)?(?:\/|$)/.test(t)}}function $(t){const e=t?.apiKey?.trim()||f.GOOGLE_CLOUD_API_KEY?.trim();if(!(!e||e===V||q(e)))return e}function q(t){return/^<[^>]+>$/.test(t)}function F(t){const e=t?.project||f.GOOGLE_CLOUD_PROJECT||f.GCLOUD_PROJECT;if(!e)throw new Error("Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT or pass project in options.");return e}function z(t){const e=t?.location||f.GOOGLE_CLOUD_LOCATION;if(!e)throw new Error("Vertex AI requires a location. Set GOOGLE_CLOUD_LOCATION or pass location in options.");return e}function X(t,e,n={}){const a=R(t,e),i={};n.temperature!==void 0&&(i.temperature=n.temperature),n.maxTokens!==void 0&&(i.maxOutputTokens=n.maxTokens);const s={...Object.keys(i).length>0&&i,...e.systemPrompt&&{systemInstruction:U(e.systemPrompt)},...e.tools&&e.tools.length>0&&{tools:M(e.tools)}};if(e.tools&&e.tools.length>0&&n.toolChoice?s.toolConfig={functionCallingConfig:{mode:G(n.toolChoice)}}:s.toolConfig=void 0,n.thinking?.enabled&&t.reasoning){const g={includeThoughts:!0};n.thinking.level!==void 0?g.thinkingLevel=H[n.thinking.level]:n.thinking.budgetTokens!==void 0&&(g.thinkingBudget=n.thinking.budgetTokens),s.thinkingConfig=g}else t.reasoning&&n.thinking&&!n.thinking.enabled&&(s.thinkingConfig=Y(t));if(n.signal){if(n.signal.aborted)throw new Error("Request aborted");s.abortSignal=n.signal}return{model:t.id,contents:a,config:s}}function C(t){return/gemini-3(?:\.\d+)?-pro/.test(t.id.toLowerCase())}function b(t){return/gemini-3(?:\.\d+)?-flash/.test(t.id.toLowerCase())}function Y(t){const e=t;return C(e)?{thinkingLevel:d.LOW}:b(e)?{thinkingLevel:d.MINIMAL}:{thinkingBudget:0}}function Q(t,e){if(C(e))switch(t){case"minimal":case"low":return"LOW";case"medium":case"high":return"HIGH"}switch(t){case"minimal":return"MINIMAL";case"low":return"LOW";case"medium":return"MEDIUM";case"high":return"HIGH"}}function Z(t,e,n){return n?.[e]!==void 0?n[e]:t.id.includes("2.5-pro")?{minimal:128,low:2048,medium:8192,high:32768}[e]:t.id.includes("2.5-flash")?{minimal:128,low:2048,medium:8192,high:24576}[e]:-1}export{k as streamGoogleVertex,rt as streamSimpleGoogleVertex};
|
dist/assets/hash-DMDecQcg.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
function l(a){let t=3735928559,h=1103547991;for(let e=0;e<a.length;e++){const i=a.charCodeAt(e);t=Math.imul(t^i,2654435761),h=Math.imul(h^i,1597334677)}return t=Math.imul(t^t>>>16,2246822507)^Math.imul(h^h>>>13,3266489909),h=Math.imul(h^h>>>16,2246822507)^Math.imul(t^t>>>13,3266489909),(h>>>0).toString(36)+(t>>>0).toString(36)}export{l as s};
|
dist/assets/headers-CgnjaPPL.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
function n(o){const e={};for(const[r,t]of o.entries())e[r]=t;return e}export{n as h};
|
dist/assets/index-BCicI0Zo.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dist/assets/index-Brjf2SFV.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const Q="https://stackblitz.com",X=new Error;X.stack="";const x={};let g=null;const v={get editorOrigin(){return g==null&&(g=new URL(globalThis.WEBCONTAINER_API_IFRAME_URL??Q).origin),g},set editorOrigin(e){g=new URL(e).origin},setQueryParam(e,t){x[e]=t},get url(){const e=new URL(this.editorOrigin);e.pathname="/headless";for(const t in x)e.searchParams.set(t,x[t]);return e.searchParams.set("version","1.6.4"),e}};function $(){let e,t;function r(){t=new Promise(n=>e=n)}return r(),{get promise(){return t},resolve(n){return e(n)},reset:r}}$();var _;(function(e){e.UncaughtException="PREVIEW_UNCAUGHT_EXCEPTION",e.UnhandledRejection="PREVIEW_UNHANDLED_REJECTION",e.ConsoleError="PREVIEW_CONSOLE_ERROR"})(_||(_={}));var K=Object.defineProperty,Z=(e,t)=>{for(var r in t)K(e,r,{get:t[r],enumerable:!0})},d={};Z(d,{createEndpoint:()=>M,expose:()=>C,proxy:()=>H,proxyMarker:()=>O,releaseProxy:()=>T,transfer:()=>W,transferHandlers:()=>A,windowEndpoint:()=>se,wrap:()=>z});var O=Symbol("Comlink.proxy"),M=Symbol("Comlink.endpoint"),T=Symbol("Comlink.releaseProxy"),I=Symbol("Comlink.thrown"),j=e=>typeof e=="object"&&e!==null||typeof e=="function",ee={canHandle:e=>j(e)&&e[O],serialize(e){const{port1:t,port2:r}=new MessageChannel;return C(e,t),[r,[r]]},deserialize(e){return e.start(),z(e)}},te={canHandle:e=>j(e)&&I in e,serialize({value:e}){let t;return e instanceof Error?t={isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:t={isError:!1,value:e},[t,[]]},deserialize(e){throw e.isError?Object.assign(new Error(e.value.message),e.value):e.value}},A=new Map([["proxy",ee],["throw",te]]);function C(e,t=self){t.addEventListener("message",function r(n){if(!n||!n.data)return;const{id:s,type:i,path:o}=Object.assign({path:[]},n.data),a=(n.data.argumentList||[]).map(m);let c;try{const l=o.slice(0,-1).reduce((u,w)=>u[w],e),f=o.reduce((u,w)=>u[w],e);switch(i){case 0:c=f;break;case 1:l[o.slice(-1)[0]]=m(n.data.value),c=!0;break;case 2:c=f.apply(l,a);break;case 3:{const u=new f(...a);c=H(u)}break;case 4:{const{port1:u,port2:w}=new MessageChannel;C(e,w),c=W(u,[u])}break;case 5:c=void 0;break}}catch(l){c={value:l,[I]:0}}Promise.resolve(c).catch(l=>({value:l,[I]:0})).then(l=>{const[f,u]=N(l);t.postMessage(Object.assign(Object.assign({},f),{id:s}),u),i===5&&(t.removeEventListener("message",r),D(t))})}),t.start&&t.start()}function re(e){return e.constructor.name==="MessagePort"}function D(e){re(e)&&e.close()}function z(e,t){return L(e,[],t)}function b(e){if(e)throw new Error("Proxy has been released and is not useable")}function L(e,t=[],r=function(){}){let n=!1;const s=new Proxy(r,{get(i,o){if(b(n),o===T)return()=>p(e,{type:5,path:t.map(a=>a.toString())}).then(()=>{D(e),n=!0});if(o==="then"){if(t.length===0)return{then:()=>s};const a=p(e,{type:0,path:t.map(c=>c.toString())}).then(m);return a.then.bind(a)}return L(e,[...t,o])},set(i,o,a){b(n);const[c,l]=N(a);return p(e,{type:1,path:[...t,o].map(f=>f.toString()),value:c},l).then(m)},apply(i,o,a){b(n);const c=t[t.length-1];if(c===M)return p(e,{type:4}).then(m);if(c==="bind")return L(e,t.slice(0,-1));const[l,f]=F(a);return p(e,{type:2,path:t.map(u=>u.toString()),argumentList:l},f).then(m)},construct(i,o){b(n);const[a,c]=F(o);return p(e,{type:3,path:t.map(l=>l.toString()),argumentList:a},c).then(m)}});return s}function ne(e){return Array.prototype.concat.apply([],e)}function F(e){const t=e.map(N);return[t.map(r=>r[0]),ne(t.map(r=>r[1]))]}var V=new WeakMap;function W(e,t){return V.set(e,t),e}function H(e){return Object.assign(e,{[O]:!0})}function se(e,t=self,r="*"){return{postMessage:(n,s)=>e.postMessage(n,r,s),addEventListener:t.addEventListener.bind(t),removeEventListener:t.removeEventListener.bind(t)}}function N(e){for(const[t,r]of A)if(r.canHandle(e)){const[n,s]=r.serialize(e);return[{type:3,name:t,value:n},s]}return[{type:0,value:e},V.get(e)||[]]}function m(e){switch(e.type){case 3:return A.get(e.name).deserialize(e.value);case 0:return e.value}}function p(e,t,r){return new Promise(n=>{const s=ie();e.addEventListener("message",function i(o){!o.data||!o.data.id||o.data.id!==s||(e.removeEventListener("message",i),n(o.data))}),e.start&&e.start(),e.postMessage(Object.assign({id:s},t),r)})}function ie(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}const oe=[_.ConsoleError,_.UncaughtException,_.UnhandledRejection];function ae(e){return!(e==null||typeof e!="object"||!("type"in e)||!oe.includes(e.type))}function y(e){const t=Object.create(null);return e?Object.assign(t,e):t}function B(e){const t={d:{}};for(const r of Object.keys(e)){const n=e[r];if("file"in n){if("symlink"in n.file){t.d[r]={f:{l:n.file.symlink}};continue}const i=n.file.contents,o=typeof i=="string"?i:le(i),a=typeof i=="string"?{}:{b:!0};t.d[r]={f:{c:o,...a}};continue}const s=B(n.directory);t.d[r]=s}return t}function G(e){const t=y();if("f"in e)throw new Error("It is not possible to export a single file in the JSON format.");if("d"in e)for(const r of Object.keys(e.d)){const n=e.d[r];"d"in n?t[r]=y({directory:G(n)}):"f"in n&&("c"in n.f?t[r]=y({file:y({contents:n.f.b?ce(n.f.c):n.f.c})}):"l"in n.f&&(t[r]=y({file:y({symlink:n.f.l})})))}return t}function ce(e){const t=new Uint8Array(e.length);for(let r=0;r<e.length;r++)t[r]=e[r].charCodeAt(0);return t}function le(e){let t="";for(let r=0;r<e.length;r++)t+=String.fromCharCode(e[r]);return t}let E=null,P=null,k={};const Y=new TextDecoder,ue=new TextEncoder;class h{_instance;_runtimeInfo;fs;static _instance=null;static _teardownPromise=null;_tornDown=!1;_unsubscribeFromTokenChangedListener=()=>{};constructor(t,r,n,s){this._instance=t,this._runtimeInfo=s,this.fs=new pe(r)}async spawn(t,r,n){let s=[];Array.isArray(r)?s=r:n=r;let i,o=new ReadableStream;if(n?.output!==!1){const U=Ee();i=U.push,o=U.stream}let a,c,l,f;const u=S(R(i)),w=S(R(a)),q=S(R(l)),J=await this._instance.run({command:t,args:s,cwd:n?.cwd,env:n?.env,terminal:n?.terminal},w,q,u);return new me(J,o,c,f)}async export(t,r){const n={format:r?.format??"json",includes:r?.includes,excludes:r?.excludes,external:!0},s=await this._instance.serialize(t,n);if(n.format==="json"){const i=JSON.parse(Y.decode(s));return G(i)}return s}on(t,r){if(t==="preview-message"){const i=r;r=(o=>{ae(o)&&i(o)})}const{listener:n,subscribe:s}=Pe(r);return s(this._instance.on(t,d.proxy(n)))}mount(t,r){const n=t instanceof Uint8Array?t:t instanceof ArrayBuffer?new Uint8Array(t):ue.encode(JSON.stringify(B(t)));return this._instance.loadFiles(d.transfer(n,[n.buffer]),{mountPoints:r?.mountPoint})}setPreviewScript(t,r){return this._instance.setPreviewScript(t,r)}get path(){return this._runtimeInfo.path}get workdir(){return this._runtimeInfo.cwd}teardown(){if(this._tornDown)throw new Error("WebContainer already torn down");this._tornDown=!0,this._unsubscribeFromTokenChangedListener();const t=async()=>{try{await this.fs._teardown(),await this._instance.teardown()}finally{this._instance[d.releaseProxy](),h._instance===this&&(h._instance=null)}};h._teardownPromise=t()}static async boot(t={}){await this._teardownPromise,h._teardownPromise=null;const{workdirName:r}=t;if(window.crossOriginIsolated&&t.coep==="none"&&console.warn(`A Cross-Origin-Embedder-Policy header is required in cross origin isolated environments.
|
| 2 |
+
Set the 'coep' option to 'require-corp'.`),r?.includes("/")||r===".."||r===".")throw new Error("workdirName should be a valid folder name");for(;E;)await E;if(h._instance)throw new Error("Only a single WebContainer instance can be booted");const n=ye(t);E=n.catch(()=>{});try{const s=await n;return h._instance=s,s}finally{E=null}}}const fe=1,de=2;class he{name;_type;constructor(t,r){this.name=t,this._type=r}isFile(){return this._type===fe}isDirectory(){return this._type===de}}class we{_apiClient;_path;_options;_listener;_wrappedListener;_watcher;_closed=!1;constructor(t,r,n,s){this._apiClient=t,this._path=r,this._options=n,this._listener=s,this._apiClient._watchers.add(this),this._wrappedListener=(i,o)=>{this._listener&&!this._closed&&this._listener(i,o)},this._apiClient._fs.watch(this._path,this._options,S(this._wrappedListener)).then(i=>{if(this._watcher=i,this._closed)return this._teardown()}).catch(console.error)}async close(){this._closed||(this._closed=!0,this._apiClient._watchers.delete(this),await this._teardown())}async _teardown(){await this._watcher?.close().finally(()=>{this._watcher?.[d.releaseProxy]()})}}class me{output;input;exit;_process;stdout;stderr;constructor(t,r,n,s){this.output=r,this._process=t,this.input=new WritableStream({write:i=>{this._getProcess()?.write(i).catch(()=>{})}}),this.exit=this._onExit(),this.stdout=n,this.stderr=s}kill(){this._process?.kill()}resize(t){this._getProcess()?.resize(t)}async _onExit(){try{return await this._process.onExit}finally{this._process?.[d.releaseProxy](),this._process=null}}_getProcess(){return this._process==null&&console.warn("This process already exited"),this._process}}class pe{_fs;_watchers=new Set([]);constructor(t){this._fs=t}rm(...t){return this._fs.rm(...t)}async readFile(t,r){return await this._fs.readFile(t,r)}async rename(t,r){return await this._fs.rename(t,r)}async writeFile(t,r,n){if(r instanceof Uint8Array){const s=r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength);r=d.transfer(new Uint8Array(s),[s])}await this._fs.writeFile(t,r,n)}async readdir(t,r){const n=await this._fs.readdir(t,r);return ge(n)||be(n)?n:n.map(i=>new he(i.name,i["Symbol(type)"]))}async mkdir(t,r){return await this._fs.mkdir(t,r)}watch(t,r,n){return typeof r=="function"&&(n=r,r=null),new we(this,t,r,n)}async _teardown(){this._fs[d.releaseProxy](),await Promise.all([...this._watchers].map(t=>t.close()))}}async function ye(e){const{serverPromise:t}=_e(e),n=await(await t).build({host:window.location.host,version:"1.6.4",workdirName:e.workdirName,forwardPreviewErrors:e.forwardPreviewErrors}),[s,i,o]=await Promise.all([n.fs(),n.previewScript(),n.runtimeInfo()]);return new h(n,s,i,o)}function R(e){if(e!=null)return t=>{t instanceof Uint8Array?e(Y.decode(t)):t==null&&e(null)}}function S(e){if(e!=null)return d.proxy(e)}function _e(e){if(P!=null)return e.coep!==k.coep&&(console.warn(`Attempting to boot WebContainer with 'coep: ${e.coep}'`),console.warn(`First boot had 'coep: ${k.coep}', new settings will not take effect!`)),{serverPromise:P};e.coep&&v.setQueryParam("coep",e.coep),e.experimentalNode&&v.setQueryParam("experimental_node","1");const t=document.createElement("iframe");t.style.display="none",t.setAttribute("allow","cross-origin-isolated");const r=v.url;t.src=r.toString();const{origin:n}=r;return k={...e},P=new Promise(s=>{const i=o=>{if(o.origin!==n)return;const{data:a}=o;if(a.type==="init"){s(d.wrap(o.ports[0]));return}if(a.type==="warning"){console[a.level].call(console,a.message);return}};window.addEventListener("message",i)}),document.body.insertBefore(t,null),{serverPromise:P}}function ge(e){return typeof e[0]=="string"}function be(e){return e[0]instanceof Uint8Array}function Ee(){let e=null;return{stream:new ReadableStream({start(n){e=n}}),push:n=>{n!=null?e?.enqueue(n):(e?.close(),e=null)}}}function Pe(e){let t=!1,r=()=>{};return{subscribe(s){return s.then(i=>{r=i,t&&r()}),()=>{t=!0,r()}},listener:((...s)=>{t||e(...s)})}}export{_ as PreviewMessageType,h as WebContainer,ae as isPreviewMessage};
|
dist/assets/index-CRS_ZGq-.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
:root{color:#172033;background:#f3f6f9;font-family:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,sans-serif}*{box-sizing:border-box}body{margin:0;min-width:320px}button,input,select,textarea{font:inherit}#app{min-height:100vh;padding:18px}.shell{width:min(1280px,100%);margin:0 auto}.topbar{display:flex;align-items:end;justify-content:space-between;gap:16px;padding:8px 0 18px;border-bottom:1px solid #c7d2df}h1,h2,p{margin:0}h1{font-size:38px;line-height:1;letter-spacing:0}h2{font-size:15px;line-height:1.2;color:#405064}#model-label{margin-top:8px;color:#5d6c7f;overflow-wrap:anywhere}.status-stack{display:flex;flex-wrap:wrap;justify-content:end;gap:8px;max-width:620px}.status{min-height:34px;max-width:260px;padding:7px 10px;border:1px solid #b7c4d3;border-radius:6px;background:#fff;color:#314155;overflow-wrap:anywhere}.controls{display:grid;grid-template-columns:1.2fr 1fr 1fr 1fr;gap:12px;margin:18px 0 12px}label{display:grid;gap:6px;color:#526173;font-size:14px;font-weight:650}select,input,textarea{width:100%;border:1px solid #b8c5d4;border-radius:6px;background:#fff;color:#152033}select,input{height:42px;padding:0 10px}.command-row{display:flex;flex-wrap:wrap;gap:10px;margin-bottom:14px}button{min-width:116px;height:42px;padding:0 16px;border:0;border-radius:6px;background:#176b6c;color:#fff;font-weight:750;cursor:pointer}button:nth-child(2){background:#4f5f73}button:nth-child(3){background:#7a4d18}button:disabled{cursor:wait;opacity:.64}.workspace{display:grid;gap:14px}.prompt-pane{display:grid;gap:10px}textarea{min-height:116px;resize:vertical;padding:13px;line-height:1.45}#run{width:fit-content}.panes{display:grid;grid-template-columns:1.35fr .9fr;gap:12px}.pane{display:grid;gap:8px;min-width:0}.pane.wide{grid-column:1 / -1}pre{min-height:220px;max-height:360px;margin:0;padding:14px;border:1px solid #b8c5d4;border-radius:6px;background:#fbfcfd;color:#162033;line-height:1.45;white-space:pre-wrap;overflow:auto;overflow-wrap:anywhere}#event-log{min-height:150px;max-height:220px}@media(max-width:780px){#app{padding:12px}.topbar{align-items:stretch;flex-direction:column}.status-stack{justify-content:start}.controls,.panes{grid-template-columns:1fr}}
|
dist/assets/json-parse-B9IFJnx2.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import{d as o}from"./index-BCicI0Zo.js";const a=new Set(['"',"\\","/","b","f","n","r","t","u"]);function f(t){const r=t.codePointAt(0);return r!==void 0&&r>=0&&r<=31}function d(t){switch(t){case"\b":return"\\b";case"\f":return"\\f";case`
|
| 2 |
+
`:return"\\n";case"\r":return"\\r";case" ":return"\\t";default:return`\\u${t.codePointAt(0)?.toString(16).padStart(4,"0")??"0000"}`}}function s(t){let r="",c=!1;for(let e=0;e<t.length;e++){const n=t[e];if(!c){r+=n,n==='"'&&(c=!0);continue}if(n==='"'){r+=n,c=!1;continue}if(n==="\\"){const i=t[e+1];if(i===void 0){r+="\\\\";continue}if(i==="u"){const u=t.slice(e+2,e+6);if(/^[0-9a-fA-F]{4}$/.test(u)){r+=`\\u${u}`,e+=5;continue}}if(a.has(i)){r+=`\\${i}`,e+=1;continue}r+="\\\\";continue}r+=f(n)?d(n):n}return r}function h(t){try{return JSON.parse(t)}catch(r){const c=s(t);if(c!==t)return JSON.parse(c);throw r}}function l(t){if(!t||t.trim()==="")return{};try{return h(t)}catch{try{return o.parse(t)??{}}catch{try{return o.parse(s(t))??{}}catch{return{}}}}}export{l as a,h as p};
|
dist/assets/mistral-DpUPGD8Y.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dist/assets/openai-codex-responses-j10R-njq.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import{A as ae,g as j,a as ie}from"./index-BCicI0Zo.js";import{h as X}from"./headers-CgnjaPPL.js";import{c as ce}from"./openai-prompt-cache-h_V5PL7P.js";import{c as V,a as ue,p as Y}from"./openai-responses-shared-DeOhQuRA.js";import{b as le}from"./transform-messages-Bxe2XUr4.js";import"./hash-DMDecQcg.js";import"./json-parse-B9IFJnx2.js";import"./sanitize-unicode-B62XMGgN.js";const $=new Set;function de(e){return $.add(e),()=>{$.delete(e)}}function A(e){return e instanceof Error?e.message||e.name:typeof e=="string"?e:String(e)}function fe(e){if(!(e instanceof Error))return{name:"ThrownValue",message:A(e)};const r=e.code;return{name:e.name||void 0,message:e.message||e.name,stack:e.stack,code:typeof r=="string"||typeof r=="number"?r:void 0}}function pe(e,r,t){return{type:e,timestamp:Date.now(),error:fe(r),details:t}}function ye(e,r){e.diagnostics=[...e.diagnostics??[],r]}var R={},me=function(e,r){return typeof e=="string"&&/^\.\.?\//.test(e)?e.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i,function(t,s,o,n,i){return s?".js":o&&(!n||!i)?t:o+n+"."+i.toLowerCase()+"js"}):e};let v=null;const z=e=>import(me(e)),be="node:os";typeof process<"u"&&(process.versions?.node||process.versions?.bun)&&z(be).then(e=>{v=e});const we="https://chatgpt.com/backend-api",he="https://api.openai.com/auth",W=3,F=1e3,G=new Set(["openai","openai-codex","opencode"]),ge=1009,Se=new Set(["completed","incomplete","failed","cancelled","queued","in_progress"]);function Ee(e,r){return e===429||e===500||e===502||e===503||e===504?!0:/rate.?limit|overloaded|service.?unavailable|upstream.?connect|connection.?refused/i.test(r)}function J(e,r){return new Promise((t,s)=>{if(r?.aborted){s(new Error("Request was aborted"));return}const o=setTimeout(t,e);r?.addEventListener("abort",()=>{clearTimeout(o),s(new Error("Request was aborted"))})})}const ve=(e,r,t)=>{const s=new ae;return(async()=>{const o={role:"assistant",content:[],api:"openai-codex-responses",provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const n=t?.apiKey||j(e.provider)||"";if(!n)throw new Error(`No API key for provider: ${e.provider}`);const i=Xe(n);let a=ke(e,r,t);const u=await t?.onPayload?.(a,e);u!==void 0&&(a=u);const l=t?.sessionId||Ve(),w=Ye(e.headers,t?.headers,i,n,t?.sessionId),g=ze(e.headers,t?.headers,i,n,l),d=JSON.stringify(a),b=t?.transport||"auto",f=b!=="sse"&&re(t?.sessionId);if(f&&U(t?.sessionId),b!=="sse"&&!f){let m=!1;try{if(await Ke(xe(e.baseUrl),a,g,o,s,e,()=>{m=!0},t),t?.signal?.aborted)throw new Error("Request was aborted");s.push({type:"done",reason:o.stopReason,message:o}),s.end();return}catch(p){if(t?.signal?.aborted||Te(p)||(ye(o,pe("provider_transport_failure",p,{configuredTransport:b,fallbackTransport:m?void 0:"sse",eventsEmitted:m,phase:m?"after_message_stream_start":"before_message_stream_start",requestBytes:new TextEncoder().encode(d).byteLength})),Le(t?.sessionId,p),m))throw p;U(t?.sessionId)}}let c,y;for(let m=0;m<=W;m++){if(t?.signal?.aborted)throw new Error("Request was aborted");try{if(c=await fetch(ee(e.baseUrl),{method:"POST",headers:w,body:d,signal:t?.signal}),await t?.onResponse?.({status:c.status,headers:X(c.headers)},e),c.ok)break;const p=await c.text();if(m<W&&Ee(c.status,p)){let x=F*2**m;const P=c.headers.get("retry-after-ms");if(P!==null){const E=Number(P);Number.isFinite(E)&&(x=Math.max(0,E))}else{const E=c.headers.get("retry-after");if(E){const B=Number(E);if(Number.isFinite(B))x=Math.max(0,B*1e3);else{const M=Date.parse(E);Number.isNaN(M)||(x=Math.max(0,M-Date.now()))}}}await J(x,t?.signal);continue}const _=new Response(p,{status:c.status,statusText:c.statusText}),N=await je(_);throw new Error(N.friendlyMessage||N.message)}catch(p){if(p instanceof Error&&(p.name==="AbortError"||p.message==="Request was aborted"))throw new Error("Request was aborted");if(y=p instanceof Error?p:new Error(String(p)),m<W&&!y.message.includes("usage limit")){const _=F*2**m;await J(_,t?.signal);continue}throw y}}if(!c?.ok)throw y??new Error("Failed after retries");if(!c.body)throw new Error("No response body");if(s.push({type:"start",partial:o}),await Re(c,o,s,e,t),t?.signal?.aborted)throw new Error("Request was aborted");s.push({type:"done",reason:o.stopReason,message:o}),s.end()}catch(n){for(const i of o.content)delete i.partialJson;o.stopReason=t?.signal?.aborted?"aborted":"error",o.errorMessage=n instanceof Error?n.message:String(n),s.push({type:"error",reason:o.stopReason,error:o}),s.end()}})(),s},nt=(e,r,t)=>{const s=t?.apiKey||j(e.provider);if(!s)throw new Error(`No API key for provider: ${e.provider}`);const o=le(e,t,s),n=t?.reasoning?ie(e,t.reasoning):void 0;return ve(e,r,{...o,reasoningEffort:n==="off"?void 0:n})};function ke(e,r,t){const s=V(e,r,G,{includeSystemPrompt:!1}),o={model:e.id,store:!1,stream:!0,instructions:r.systemPrompt||"You are a helpful assistant.",input:s,text:{verbosity:t?.textVerbosity||"low"},include:["reasoning.encrypted_content"],prompt_cache_key:ce(t?.sessionId),tool_choice:"auto",parallel_tool_calls:!0};if(t?.temperature!==void 0&&(o.temperature=t.temperature),t?.serviceTier!==void 0&&(o.service_tier=t.serviceTier),r.tools&&r.tools.length>0&&(o.tools=ue(r.tools,{strict:null})),t?.reasoningEffort!==void 0){const n=t.reasoningEffort==="none"?e.thinkingLevelMap?.off??"none":e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort;n!==null&&(o.reasoning={effort:n,summary:t.reasoningSummary??"auto"})}return o}function _e(e,r){switch(r){case"flex":return .5;case"priority":return e.id==="gpt-5.5"?2.5:2;default:return 1}}function Q(e,r,t){const s=_e(t,r);s!==1&&(e.cost.input*=s,e.cost.output*=s,e.cost.cacheRead*=s,e.cost.cacheWrite*=s,e.cost.total=e.cost.input+e.cost.output+e.cost.cacheRead+e.cost.cacheWrite)}function Z(e,r){return e==="default"&&(r==="flex"||r==="priority")?r:e??r}function ee(e){const t=(e&&e.trim().length>0?e:we).replace(/\/+$/,"");return t.endsWith("/codex/responses")?t:t.endsWith("/codex")?`${t}/responses`:`${t}/codex/responses`}function xe(e){const r=new URL(ee(e));return r.protocol==="https:"&&(r.protocol="wss:"),r.protocol==="http:"&&(r.protocol="ws:"),r.toString()}async function Re(e,r,t,s,o){await Y(te(Oe(e)),r,t,s,{serviceTier:o?.serviceTier,resolveServiceTier:Z,applyServiceTierPricing:(n,i)=>Q(n,i,s)})}class L extends Error{code;payload;constructor(r,t){super(r),this.name="CodexApiError",this.code=t?.code,this.payload=t?.payload,this.cause=t?.cause}}class q extends Error{payload;constructor(r,t){super(r),this.name="CodexProtocolError",this.payload=t?.payload,this.cause=t?.cause}}function Te(e){return e instanceof L||e instanceof q}async function*te(e){for await(const r of e){const t=typeof r.type=="string"?r.type:void 0;if(t){if(t==="error"){const s=r.code||"",o=r.message||"";throw new L(`Codex error: ${o||s||JSON.stringify(r)}`,{code:s||void 0,payload:r})}if(t==="response.failed"){const s=r.response,o=s?.error?.code,n=s?.error?.message;throw new L(n||"Codex response failed",{code:o,payload:r})}if(t==="response.done"||t==="response.completed"||t==="response.incomplete"){const s=r.response,o=s&&{...s,status:Ce(s.status)};yield{...r,type:"response.completed",response:o};return}yield r}}}function Ce(e){if(typeof e=="string")return Se.has(e)?e:void 0}async function*Oe(e){if(!e.body)return;const r=e.body.getReader(),t=new TextDecoder;let s="";try{for(;;){const{done:o,value:n}=await r.read();if(o)break;s+=t.decode(n,{stream:!0});let i=s.indexOf(`
|
| 2 |
+
|
| 3 |
+
`);for(;i!==-1;){const a=s.slice(0,i);s=s.slice(i+2);const u=a.split(`
|
| 4 |
+
`).filter(l=>l.startsWith("data:")).map(l=>l.slice(5).trim());if(u.length>0){const l=u.join(`
|
| 5 |
+
`).trim();if(l&&l!=="[DONE]")try{yield JSON.parse(l)}catch(w){throw new q(`Invalid Codex SSE JSON: ${A(w)}`,{cause:w,payload:l})}}i=s.indexOf(`
|
| 6 |
+
|
| 7 |
+
`)}}}finally{try{await r.cancel()}catch{}try{r.releaseLock()}catch{}}}const Ae="responses_websockets=2026-02-06",We=300*1e3,h=new Map,k=new Map,O=new Set;function D(e){let r=k.get(e);return r||(r={requests:0,connectionsCreated:0,connectionsReused:0,cachedContextRequests:0,storeTrueRequests:0,fullContextRequests:0,deltaRequests:0,lastInputItems:0,websocketFailures:0,sseFallbacks:0},k.set(e,r)),r}function at(e){const r=k.get(e);return r?{...r}:void 0}function it(e){if(e){k.delete(e),O.delete(e);return}k.clear(),O.clear()}function Ie(e){const r=t=>{t.idleTimer&&clearTimeout(t.idleTimer),S(t.socket,1e3,"debug_close")};if(e){const t=h.get(e);t&&r(t),h.delete(e);return}for(const t of h.values())r(t);h.clear()}de(Ie);function re(e){return e?O.has(e):!1}function U(e){if(!e)return;const r=D(e);r.sseFallbacks++,r.websocketFallbackActive=re(e)}function Le(e,r){if(!e)return;O.add(e);const t=D(e);t.websocketFailures++,t.lastWebSocketError=A(r),t.websocketFallbackActive=!0}let T=null;async function qe(){if(T)return T;if(process?.versions?.bun&&(R.HTTP_PROXY||R.HTTPS_PROXY||R.http_proxy||R.https_proxy)){const t=(await z("proxy-from-env")).getProxyForUrl;return T=class extends WebSocket{constructor(s,o){let n={};Array.isArray(o)||typeof o=="string"?n={protocols:o}:n={...o};const i=t(s.toString().replace(/^wss:/,"https:").replace(/^ws:/,"http:"));super(s,{...n,...i?{proxy:i}:{}})}},T}const e=globalThis.WebSocket;return typeof e!="function"?null:e}class De extends Error{code;reason;wasClean;constructor(r,t){super(r),this.name="WebSocketCloseError",this.code=t?.code,this.reason=t?.reason,this.wasClean=t?.wasClean}}function Ne(e){const r=e.readyState;return typeof r=="number"?r:void 0}function C(e){const r=Ne(e);return r===void 0||r===1}function S(e,r=1e3,t="done"){try{e.close(r,t)}catch{}}function H(e,r){r.idleTimer&&clearTimeout(r.idleTimer),r.idleTimer=setTimeout(()=>{r.busy||(S(r.socket,1e3,"idle_timeout"),h.delete(e))},We)}async function I(e,r,t){const s=await qe();if(!s)throw new Error("WebSocket transport is not available in this runtime");const o=X(r);return delete o["OpenAI-Beta"],new Promise((n,i)=>{let a=!1,u;try{u=new s(e,{headers:o})}catch(f){i(f instanceof Error?f:new Error(String(f)));return}const l=()=>{a||(a=!0,b(),n(u))},w=f=>{const c=se(f);a||(a=!0,b(),i(c))},g=f=>{const c=oe(f);a||(a=!0,b(),i(c))},d=()=>{a||(a=!0,b(),u.close(1e3,"aborted"),i(new Error("Request was aborted")))},b=()=>{u.removeEventListener("open",l),u.removeEventListener("error",w),u.removeEventListener("close",g),t?.removeEventListener("abort",d)};u.addEventListener("open",l),u.addEventListener("error",w),u.addEventListener("close",g),t?.addEventListener("abort",d)})}async function Pe(e,r,t,s){if(!t){const a=await I(e,r,s);return{socket:a,reused:!1,release:({keep:u}={})=>{if(u===!1){S(a);return}S(a)}}}const o=h.get(t);if(o){if(o.idleTimer&&(clearTimeout(o.idleTimer),o.idleTimer=void 0),!o.busy&&C(o.socket))return o.busy=!0,{socket:o.socket,entry:o,reused:!0,release:({keep:a}={})=>{if(!a||!C(o.socket)){S(o.socket),h.delete(t);return}o.busy=!1,H(t,o)}};if(o.busy){const a=await I(e,r,s);return{socket:a,reused:!1,release:()=>{S(a)}}}C(o.socket)||(S(o.socket),h.delete(t))}const n=await I(e,r,s),i={socket:n,busy:!0};return h.set(t,i),{socket:n,entry:i,reused:!1,release:({keep:a}={})=>{if(!a||!C(i.socket)){S(i.socket),i.idleTimer&&clearTimeout(i.idleTimer),h.get(t)===i&&h.delete(t);return}i.busy=!1,H(t,i)}}}function se(e){if(e&&typeof e=="object"){const r="message"in e?e.message:void 0;if(typeof r=="string"&&r.length>0)return new Error(r);const t="error"in e?e.error:void 0;if(t instanceof Error&&t.message.length>0)return t;if(t&&typeof t=="object"&&"message"in t){const s=t.message;if(typeof s=="string"&&s.length>0)return new Error(s)}}return new Error("WebSocket error")}function oe(e){if(e&&typeof e=="object"){const r="code"in e?e.code:void 0,t="reason"in e?e.reason:void 0,s="wasClean"in e?e.wasClean:void 0,o=typeof r=="number"?` ${r}`:"";let n=typeof t=="string"&&t.length>0?` ${t}`:"";return!n&&r===ge&&(n=" message too big"),new De(`WebSocket closed${o}${n}`.trim(),{code:typeof r=="number"?r:void 0,reason:typeof t=="string"&&t.length>0?t:void 0,wasClean:typeof s=="boolean"?s:void 0})}return new Error("WebSocket closed")}async function Be(e){if(typeof e=="string")return e;if(e instanceof ArrayBuffer)return new TextDecoder().decode(new Uint8Array(e));if(ArrayBuffer.isView(e)){const r=e;return new TextDecoder().decode(new Uint8Array(r.buffer,r.byteOffset,r.byteLength))}if(e&&typeof e=="object"&&"arrayBuffer"in e){const t=await e.arrayBuffer();return new TextDecoder().decode(new Uint8Array(t))}return null}async function*Me(e,r){const t=[];let s=null,o=!1,n=null,i=!1;const a=()=>{if(!s)return;const d=s;s=null,d()},u=d=>{(async()=>{let b=null;try{if(!d||typeof d!="object"||!("data"in d)||(b=await Be(d.data),!b))return;const f=JSON.parse(b),c=typeof f.type=="string"?f.type:"";(c==="response.completed"||c==="response.done"||c==="response.incomplete")&&(i=!0,o=!0),t.push(f),a()}catch(f){n=new q(`Invalid Codex WebSocket JSON: ${A(f)}`,{cause:f,payload:b}),o=!0,a()}})()},l=d=>{n=se(d),o=!0,a()},w=d=>{if(i){o=!0,a();return}n||(n=oe(d)),o=!0,a()},g=()=>{n=new Error("Request was aborted"),o=!0,a()};e.addEventListener("message",u),e.addEventListener("error",l),e.addEventListener("close",w),r?.addEventListener("abort",g);try{for(;;){if(r?.aborted)throw new Error("Request was aborted");if(t.length>0){yield t.shift();continue}if(o)break;await new Promise(d=>{s=d})}if(n)throw n;if(!i)throw new Error("WebSocket stream closed before response.completed")}finally{e.removeEventListener("message",u),e.removeEventListener("error",l),e.removeEventListener("close",w),r?.removeEventListener("abort",g)}}function K(e){const{input:r,previous_response_id:t,...s}=e;return s}function $e(e,r){return JSON.stringify(e??[])===JSON.stringify(r??[])}function Fe(e,r){return JSON.stringify(K(e))===JSON.stringify(K(r))}function Je(e,r){if(!Fe(e,r.lastRequestBody))return;const t=e.input??[],s=[...r.lastRequestBody.input??[],...r.lastResponseItems];if(t.length<s.length)return;const o=t.slice(0,s.length);if($e(o,s))return t.slice(s.length)}function Ue(e,r){const t=e.continuation;if(!t)return r;const s=Je(r,t);return!s||!t.lastResponseId?(e.continuation=void 0,r):{...r,previous_response_id:t.lastResponseId,input:s}}async function*He(e,r,t,s){let o=!1;for await(const n of e)o||(o=!0,s(),t.push({type:"start",partial:r})),yield n}async function Ke(e,r,t,s,o,n,i,a){const{socket:u,entry:l,reused:w,release:g}=await Pe(e,t,a?.sessionId,a?.signal);let d=!0;const b=a?.transport==="websocket-cached"||a?.transport==="auto",f=r,c=b&&l?Ue(l,f):f,y=a?.sessionId?D(a.sessionId):void 0;y&&(y.requests++,w?y.connectionsReused++:y.connectionsCreated++,b&&y.cachedContextRequests++,c.store===!0&&y.storeTrueRequests++,y.lastInputItems=c.input?.length??0,c.previous_response_id?(y.deltaRequests++,y.lastDeltaInputItems=c.input?.length??0,y.lastPreviousResponseId=c.previous_response_id):(y.fullContextRequests++,y.lastDeltaInputItems=void 0,y.lastPreviousResponseId=void 0));try{if(u.send(JSON.stringify({type:"response.create",...c})),await Y(He(te(Me(u,a?.signal)),s,o,i),s,o,n,{serviceTier:a?.serviceTier,resolveServiceTier:Z,applyServiceTierPricing:(m,p)=>Q(m,p,n)}),a?.signal?.aborted)d=!1;else if(b&&l&&s.responseId){const m=V(n,{messages:[s]},G,{includeSystemPrompt:!1}).filter(p=>p.type!=="function_call_output");l.continuation={lastRequestBody:f,lastResponseId:s.responseId,lastResponseItems:m}}}catch(m){throw l&&(l.continuation=void 0),d=!1,m}finally{g({keep:d})}}async function je(e){const r=await e.text();let t=r||e.statusText||"Request failed",s;try{const n=JSON.parse(r)?.error;if(n){const i=n.code||n.type||"";if(/usage_limit_reached|usage_not_included|rate_limit_exceeded/i.test(i)||e.status===429){const a=n.plan_type?` (${n.plan_type.toLowerCase()} plan)`:"",u=n.resets_at?Math.max(0,Math.round((n.resets_at*1e3-Date.now())/6e4)):void 0,l=u!==void 0?` Try again in ~${u} min.`:"";s=`You have hit your ChatGPT usage limit${a}.${l}`.trim()}t=n.message||s||t}}catch{}return{message:t,friendlyMessage:s}}function Xe(e){try{const r=e.split(".");if(r.length!==3)throw new Error("Invalid token");const s=JSON.parse(atob(r[1]))?.[he]?.chatgpt_account_id;if(!s)throw new Error("No account ID in token");return s}catch{throw new Error("Failed to extract accountId from token")}}function Ve(){return typeof globalThis.crypto?.randomUUID=="function"?globalThis.crypto.randomUUID():`codex_${Date.now()}_${Math.random().toString(36).slice(2,10)}`}function ne(e,r,t,s){const o=new Headers(e);for(const[i,a]of Object.entries(r||{}))o.set(i,a);o.set("Authorization",`Bearer ${s}`),o.set("chatgpt-account-id",t),o.set("originator","pi");const n=v?`pi (${v.platform()} ${v.release()}; ${v.arch()})`:"pi (browser)";return o.set("User-Agent",n),o}function Ye(e,r,t,s,o){const n=ne(e,r,t,s);return n.set("OpenAI-Beta","responses=experimental"),n.set("accept","text/event-stream"),n.set("content-type","application/json"),o&&(n.set("session_id",o),n.set("x-client-request-id",o)),n}function ze(e,r,t,s,o){const n=ne(e,r,t,s);return n.delete("accept"),n.delete("content-type"),n.delete("OpenAI-Beta"),n.delete("openai-beta"),n.set("OpenAI-Beta",Ae),n.set("x-client-request-id",o),n.set("session_id",o),n}export{Ie as closeOpenAICodexWebSocketSessions,at as getOpenAICodexWebSocketDebugStats,it as resetOpenAICodexWebSocketDebugStats,ve as streamOpenAICodexResponses,nt as streamSimpleOpenAICodexResponses};
|
dist/assets/openai-completions-DroT7VTp.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import{O as G}from"./client-Sq9UPDIz.js";import{A as $,g as L,a as W,c as K}from"./index-BCicI0Zo.js";import{h as J}from"./headers-CgnjaPPL.js";import{a as P}from"./json-parse-B9IFJnx2.js";import{s as C}from"./sanitize-unicode-B62XMGgN.js";import{h as Y,b as V,i as Z,r as Q}from"./github-copilot-headers-CVWOSrQr.js";import{c as X}from"./openai-prompt-cache-h_V5PL7P.js";import{t as ee,b as te}from"./transform-messages-Bxe2XUr4.js";var b={};function ne(e){for(const n of e)if(n.role==="toolResult"||n.role==="assistant"&&n.content.some(t=>t.type==="toolCall"))return!0;return!1}function F(e){return e.type==="text"}function se(e){return e.type==="thinking"}function oe(e){return e.type==="toolCall"}function re(e){return e.type==="image"}function N(e){return e||(typeof process<"u"&&b.PI_CACHE_RETENTION==="long"?"long":"short")}const ae=(e,n,t)=>{const s=new $;return(async()=>{const r={role:"assistant",content:[],api:e.api,provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const c=t?.apiKey||L(e.provider)||"",u=q(e),o=N(t?.cacheRetention),i=o==="none"?void 0:t?.sessionId,p=ie(e,n,c,t?.headers,i,u);let h=le(e,n,t,u,o);const y=await t?.onPayload?.(h,e);y!==void 0&&(h=y);const R={...t?.signal?{signal:t.signal}:{},...t?.timeoutMs!==void 0?{timeout:t.timeoutMs}:{},...t?.maxRetries!==void 0?{maxRetries:t.maxRetries}:{}},{data:T,response:_}=await p.chat.completions.create(h,R).withResponse();await t?.onResponse?.({status:_.status,headers:J(_.headers)},e),s.push({type:"start",partial:r});let v=null,l=null,k=!1;const E=new Map,S=new Map,I=r.content,A=a=>I.indexOf(a),D=a=>{const f=A(a);f!==-1&&(a.type==="text"?s.push({type:"text_end",contentIndex:f,content:a.text,partial:r}):a.type==="thinking"?s.push({type:"thinking_end",contentIndex:f,content:a.thinking,partial:r}):a.type==="toolCall"&&(a.arguments=P(a.partialArgs),delete a.partialArgs,delete a.streamIndex,s.push({type:"toolcall_end",contentIndex:f,toolCall:a,partial:r})))},U=()=>(v||(v={type:"text",text:""},I.push(v),s.push({type:"text_start",contentIndex:A(v),partial:r})),v),H=a=>(l||(l={type:"thinking",thinking:"",thinkingSignature:a},I.push(l),s.push({type:"thinking_start",contentIndex:A(l),partial:r})),l),j=a=>{const f=typeof a.index=="number"?a.index:void 0;let d=f!==void 0?E.get(f):void 0;return!d&&a.id&&(d=S.get(a.id)),d||(d={type:"toolCall",id:a.id||"",name:a.function?.name||"",arguments:{},partialArgs:"",streamIndex:f},f!==void 0&&E.set(f,d),a.id&&S.set(a.id,d),I.push(d),s.push({type:"toolcall_start",contentIndex:A(d),partial:r})),f!==void 0&&d.streamIndex===void 0&&(d.streamIndex=f,E.set(f,d)),a.id&&S.set(a.id,d),d};for await(const a of T){if(!a||typeof a!="object")continue;r.responseId||=a.id,typeof a.model=="string"&&a.model.length>0&&a.model!==e.id&&(r.responseModel||=a.model),a.usage&&(r.usage=B(a.usage,e));const f=Array.isArray(a.choices)?a.choices[0]:void 0;if(f){if(!a.usage&&f.usage&&(r.usage=B(f.usage,e)),f.finish_reason){const d=ke(f.finish_reason);r.stopReason=d.stopReason,d.errorMessage&&(r.errorMessage=d.errorMessage),k=!0}if(f.delta){if(f.delta.content!==null&&f.delta.content!==void 0&&f.delta.content.length>0){const g=U();g.text+=f.delta.content,s.push({type:"text_delta",contentIndex:A(g),delta:f.delta.content,partial:r})}const d=["reasoning_content","reasoning","reasoning_text"],O=f.delta;let w=null;for(const g of d){const m=O[g];if(typeof m=="string"&&m.length>0){w=g;break}}if(w){const g=O[w];if(typeof g=="string"&&g.length>0){const m=e.provider==="opencode-go"&&w==="reasoning"?"reasoning_content":w,x=H(m);x.thinking+=g,s.push({type:"thinking_delta",contentIndex:A(x),delta:g,partial:r})}}if(f?.delta?.tool_calls)for(const g of f.delta.tool_calls){const m=j(g);!m.id&&g.id&&(m.id=g.id,S.set(g.id,m)),!m.name&&g.function?.name&&(m.name=g.function.name);let x="";g.function?.arguments&&(x=g.function.arguments,m.partialArgs=(m.partialArgs??"")+g.function.arguments,m.arguments=P(m.partialArgs)),s.push({type:"toolcall_delta",contentIndex:A(m),delta:x,partial:r})}const M=f.delta.reasoning_details;if(M&&Array.isArray(M)){for(const g of M)if(g.type==="reasoning.encrypted"&&g.id&&g.data){const m=r.content.find(x=>x.type==="toolCall"&&x.id===g.id);m&&(m.thoughtSignature=JSON.stringify(g))}}}}}for(const a of I)D(a);if(t?.signal?.aborted)throw new Error("Request was aborted");if(r.stopReason==="aborted")throw new Error("Request was aborted");if(r.stopReason==="error")throw new Error(r.errorMessage||"Provider returned an error stop reason");if(!k)throw new Error("Stream ended without finish_reason");s.push({type:"done",reason:r.stopReason,message:r}),s.end()}catch(c){for(const o of r.content)delete o.index,delete o.partialArgs,delete o.streamIndex;r.stopReason=t?.signal?.aborted?"aborted":"error",r.errorMessage=c instanceof Error?c.message:JSON.stringify(c);const u=c?.error?.metadata?.raw;u&&(r.errorMessage+=`
|
| 2 |
+
${u}`),s.push({type:"error",reason:r.stopReason,error:r}),s.end()}})(),s},Se=(e,n,t)=>{const s=t?.apiKey||L(e.provider);if(!s)throw new Error(`No API key for provider: ${e.provider}`);const r=te(e,t,s),c=t?.reasoning?W(e,t.reasoning):void 0,u=c==="off"?void 0:c,o=t?.toolChoice;return ae(e,n,{...r,reasoningEffort:u,toolChoice:o})};function ie(e,n,t,s,r,c=q(e)){if(!t){if(!b.OPENAI_API_KEY)throw new Error("OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.");t=b.OPENAI_API_KEY}const u={...e.headers};if(e.provider==="github-copilot"){const i=Y(n.messages),p=V({messages:n.messages,hasImages:i});Object.assign(u,p)}r&&c.sendSessionAffinityHeaders&&(u.session_id=r,u["x-client-request-id"]=r,u["x-session-affinity"]=r),s&&Object.assign(u,s);const o=e.provider==="cloudflare-ai-gateway"?{...u,Authorization:u.Authorization??null,"cf-aig-authorization":`Bearer ${t}`}:u;return new G({apiKey:t,baseURL:Z(e.provider)?Q(e):e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:o})}function le(e,n,t,s=q(e),r=N(t?.cacheRetention)){const c=me(e,n,s),u=ce(s,r),o={model:e.id,messages:c,stream:!0,prompt_cache_key:e.baseUrl.includes("api.openai.com")&&r!=="none"||r==="long"&&s.supportsLongCacheRetention?X(t?.sessionId):void 0,prompt_cache_retention:r==="long"&&s.supportsLongCacheRetention?"24h":void 0};if(s.supportsUsageInStreaming!==!1&&(o.stream_options={include_usage:!0}),s.supportsStore&&(o.store=!1),t?.maxTokens&&(s.maxTokensField==="max_tokens"?o.max_tokens=t.maxTokens:o.max_completion_tokens=t.maxTokens),t?.temperature!==void 0&&(o.temperature=t.temperature),n.tools&&n.tools.length>0?(o.tools=ye(n.tools,s),s.zaiToolStream&&(o.tool_stream=!0)):ne(n.messages)&&(o.tools=[]),u&&ue(c,o.tools,u),t?.toolChoice&&(o.tool_choice=t.toolChoice),s.thinkingFormat==="zai"&&e.reasoning)o.enable_thinking=!!t?.reasoningEffort;else if(s.thinkingFormat==="qwen"&&e.reasoning)o.enable_thinking=!!t?.reasoningEffort;else if(s.thinkingFormat==="qwen-chat-template"&&e.reasoning)o.chat_template_kwargs={enable_thinking:!!t?.reasoningEffort,preserve_thinking:!0};else if(s.thinkingFormat==="deepseek"&&e.reasoning)o.thinking={type:t?.reasoningEffort?"enabled":"disabled"},t?.reasoningEffort&&(o.reasoning_effort=e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort);else if(s.thinkingFormat==="openrouter"&&e.reasoning){const i=o;t?.reasoningEffort?i.reasoning={effort:e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort}:e.thinkingLevelMap?.off!==null&&(i.reasoning={effort:e.thinkingLevelMap?.off??"none"})}else if(s.thinkingFormat==="together"&&e.reasoning){const i=o;i.reasoning={enabled:!!t?.reasoningEffort},t?.reasoningEffort&&s.supportsReasoningEffort&&(i.reasoning_effort=e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort)}else if(t?.reasoningEffort&&e.reasoning&&s.supportsReasoningEffort)o.reasoning_effort=e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort;else if(!t?.reasoningEffort&&e.reasoning&&s.supportsReasoningEffort){const i=e.thinkingLevelMap?.off;typeof i=="string"&&(o.reasoning_effort=i)}if(e.baseUrl.includes("openrouter.ai")&&e.compat?.openRouterRouting&&(o.provider=e.compat.openRouterRouting),e.baseUrl.includes("ai-gateway.vercel.sh")&&e.compat?.vercelGatewayRouting){const i=e.compat.vercelGatewayRouting;if(i.only||i.order){const p={};i.only&&(p.only=i.only),i.order&&(p.order=i.order),o.providerOptions={gateway:p}}}return o}function ce(e,n){if(e.cacheControlFormat!=="anthropic"||n==="none")return;const t=n==="long"&&e.supportsLongCacheRetention?"1h":void 0;return{type:"ephemeral",...t?{ttl:t}:{}}}function ue(e,n,t){fe(e,t),ge(n,t),pe(e,t)}function fe(e,n){for(const t of e)if(t.role==="system"||t.role==="developer"){he(t,n);return}}function pe(e,n){for(let t=e.length-1;t>=0;t--){const s=e[t];if((s.role==="user"||s.role==="assistant")&&de(s,n))return}}function ge(e,n){if(!e||e.length===0)return;const t=e[e.length-1];t.cache_control=n}function he(e,n){return z(e,n)}function de(e,n){return e.role==="user"||e.role==="assistant"?z(e,n):!1}function z(e,n){const t=e.content;if(typeof t=="string")return t.length===0?!1:(e.content=[{type:"text",text:t,cache_control:n}],!0);if(!Array.isArray(t))return!1;for(let s=t.length-1;s>=0;s--){const r=t[s];if(r?.type==="text"){const c=r;return c.cache_control=n,!0}}return!1}function me(e,n,t){const s=[],r=o=>{if(o.includes("|")){const[i]=o.split("|");return i.replace(/[^a-zA-Z0-9_-]/g,"_").slice(0,40)}return e.provider==="openai"&&o.length>40?o.slice(0,40):o},c=ee(n.messages,e,o=>r(o));if(n.systemPrompt){const i=e.reasoning&&t.supportsDeveloperRole?"developer":"system";s.push({role:i,content:C(n.systemPrompt)})}let u=null;for(let o=0;o<c.length;o++){const i=c[o];if(t.requiresAssistantAfterToolResult&&u==="toolResult"&&i.role==="user"&&s.push({role:"assistant",content:"I have processed the tool results."}),i.role==="user")if(typeof i.content=="string")s.push({role:"user",content:C(i.content)});else{const p=i.content.map(h=>h.type==="text"?{type:"text",text:C(h.text)}:{type:"image_url",image_url:{url:`data:${h.mimeType};base64,${h.data}`}});if(p.length===0)continue;s.push({role:"user",content:p})}else if(i.role==="assistant"){const p={role:"assistant",content:t.requiresAssistantAfterToolResult?"":null},h=i.content.filter(F).filter(l=>l.text.trim().length>0).map(l=>({type:"text",text:C(l.text)})),y=h.map(l=>l.text).join(""),R=i.content.filter(se).filter(l=>l.thinking.trim().length>0);if(R.length>0)if(t.requiresThinkingAsText){const l=R.map(k=>C(k.thinking)).join(`
|
| 3 |
+
|
| 4 |
+
`);p.content=[{type:"text",text:l},...h]}else{y.length>0&&(p.content=y);let l=R[0].thinkingSignature;e.provider==="opencode-go"&&l==="reasoning"&&(l="reasoning_content"),l&&l.length>0&&(p[l]=R.map(k=>k.thinking).join(`
|
| 5 |
+
`))}else y.length>0&&(p.content=y);const T=i.content.filter(oe);if(T.length>0){p.tool_calls=T.map(k=>({id:k.id,type:"function",function:{name:k.name,arguments:JSON.stringify(k.arguments)}}));const l=T.filter(k=>k.thoughtSignature).map(k=>{try{return JSON.parse(k.thoughtSignature)}catch{return null}}).filter(Boolean);l.length>0&&(p.reasoning_details=l)}t.requiresReasoningContentOnAssistantMessages&&e.reasoning&&p.reasoning_content===void 0&&(p.reasoning_content="");const _=p.content;if(!(_!=null&&_.length>0)&&!p.tool_calls)continue;s.push(p)}else if(i.role==="toolResult"){const p=[];let h=o;for(;h<c.length&&c[h].role==="toolResult";h++){const y=c[h],R=y.content.filter(F).map(l=>l.text).join(`
|
| 6 |
+
`),T=y.content.some(l=>l.type==="image"),_=R.length>0,v={role:"tool",content:C(_?R:"(see attached image)"),tool_call_id:y.toolCallId};if(t.requiresToolResultName&&y.toolName&&(v.name=y.toolName),s.push(v),T&&e.input.includes("image"))for(const l of y.content)re(l)&&p.push({type:"image_url",image_url:{url:`data:${l.mimeType};base64,${l.data}`}})}o=h-1,p.length>0?(t.requiresAssistantAfterToolResult&&s.push({role:"assistant",content:"I have processed the tool results."}),s.push({role:"user",content:[{type:"text",text:"Attached image(s) from tool result:"},...p]}),u="user"):u="toolResult";continue}u=i.role}return s}function ye(e,n){return e.map(t=>({type:"function",function:{name:t.name,description:t.description,parameters:t.parameters,...n.supportsStrictMode!==!1&&{strict:!1}}}))}function B(e,n){const t=e.prompt_tokens||0,s=e.prompt_tokens_details?.cached_tokens??e.prompt_cache_hit_tokens??0,r=e.prompt_tokens_details?.cache_write_tokens||0,c=Math.max(0,t-s-r),u=e.completion_tokens||0,o={input:c,output:u,cacheRead:s,cacheWrite:r,totalTokens:c+u+s+r,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}};return K(n,o),o}function ke(e){if(e===null)return{stopReason:"stop"};switch(e){case"stop":case"end":return{stopReason:"stop"};case"length":return{stopReason:"length"};case"function_call":case"tool_calls":return{stopReason:"toolUse"};case"content_filter":return{stopReason:"error",errorMessage:"Provider finish_reason: content_filter"};case"network_error":return{stopReason:"error",errorMessage:"Provider finish_reason: network_error"};default:return{stopReason:"error",errorMessage:`Provider finish_reason: ${e}`}}}function Re(e){const n=e.provider,t=e.baseUrl,s=n==="zai"||t.includes("api.z.ai"),r=n==="together"||t.includes("api.together.ai")||t.includes("api.together.xyz"),c=n==="moonshotai"||n==="moonshotai-cn"||t.includes("api.moonshot."),u=n==="cloudflare-workers-ai"||t.includes("api.cloudflare.com"),o=n==="cloudflare-ai-gateway"||t.includes("gateway.ai.cloudflare.com"),i=n==="cerebras"||t.includes("cerebras.ai")||n==="xai"||t.includes("api.x.ai")||r||t.includes("chutes.ai")||t.includes("deepseek.com")||s||c||n==="opencode"||t.includes("opencode.ai")||u||o,p=t.includes("chutes.ai")||c||o||r,h=n==="xai"||t.includes("api.x.ai"),y=n==="deepseek"||t.includes("deepseek.com"),R=n==="openrouter"&&e.id.startsWith("anthropic/")?"anthropic":void 0;return{supportsStore:!i,supportsDeveloperRole:!i,supportsReasoningEffort:!h&&!s&&!c&&!r&&!o,supportsUsageInStreaming:!0,maxTokensField:p?"max_tokens":"max_completion_tokens",requiresToolResultName:!1,requiresAssistantAfterToolResult:!1,requiresThinkingAsText:!1,requiresReasoningContentOnAssistantMessages:y,thinkingFormat:y?"deepseek":s?"zai":r?"together":n==="openrouter"||t.includes("openrouter.ai")?"openrouter":"openai",openRouterRouting:{},vercelGatewayRouting:{},zaiToolStream:!1,supportsStrictMode:!c&&!r&&!o,cacheControlFormat:R,sendSessionAffinityHeaders:!1,supportsLongCacheRetention:!(r||u||o)}}function q(e){const n=Re(e);return e.compat?{supportsStore:e.compat.supportsStore??n.supportsStore,supportsDeveloperRole:e.compat.supportsDeveloperRole??n.supportsDeveloperRole,supportsReasoningEffort:e.compat.supportsReasoningEffort??n.supportsReasoningEffort,supportsUsageInStreaming:e.compat.supportsUsageInStreaming??n.supportsUsageInStreaming,maxTokensField:e.compat.maxTokensField??n.maxTokensField,requiresToolResultName:e.compat.requiresToolResultName??n.requiresToolResultName,requiresAssistantAfterToolResult:e.compat.requiresAssistantAfterToolResult??n.requiresAssistantAfterToolResult,requiresThinkingAsText:e.compat.requiresThinkingAsText??n.requiresThinkingAsText,requiresReasoningContentOnAssistantMessages:e.compat.requiresReasoningContentOnAssistantMessages??n.requiresReasoningContentOnAssistantMessages,thinkingFormat:e.compat.thinkingFormat??n.thinkingFormat,openRouterRouting:e.compat.openRouterRouting??{},vercelGatewayRouting:e.compat.vercelGatewayRouting??n.vercelGatewayRouting,zaiToolStream:e.compat.zaiToolStream??n.zaiToolStream,supportsStrictMode:e.compat.supportsStrictMode??n.supportsStrictMode,cacheControlFormat:e.compat.cacheControlFormat??n.cacheControlFormat,sendSessionAffinityHeaders:e.compat.sendSessionAffinityHeaders??n.sendSessionAffinityHeaders,supportsLongCacheRetention:e.compat.supportsLongCacheRetention??n.supportsLongCacheRetention}:n}export{me as convertMessages,ae as streamOpenAICompletions,Se as streamSimpleOpenAICompletions};
|
dist/assets/openai-prompt-cache-h_V5PL7P.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
function r(_){if(_===void 0)return;const n=Array.from(_);return n.length<=64?_:n.slice(0,64).join("")}export{r as c};
|
dist/assets/openai-responses-C3CNUo8J.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
import{O as A}from"./client-Sq9UPDIz.js";import{A as I,g as h,a as E}from"./index-BCicI0Zo.js";import{h as w}from"./headers-CgnjaPPL.js";import{h as _,b as O,i as b,r as P}from"./github-copilot-headers-CVWOSrQr.js";import{c as S}from"./openai-prompt-cache-h_V5PL7P.js";import{p as C,c as T,a as k}from"./openai-responses-shared-DeOhQuRA.js";import{b as L}from"./transform-messages-Bxe2XUr4.js";import"./hash-DMDecQcg.js";import"./json-parse-B9IFJnx2.js";import"./sanitize-unicode-B62XMGgN.js";var u={};const M=new Set(["openai","openai-codex","opencode"]);function g(e){return e||(typeof process<"u"&&u.PI_CACHE_RETENTION==="long"?"long":"short")}function m(e){return{sendSessionIdHeader:e.compat?.sendSessionIdHeader??!0,supportsLongCacheRetention:e.compat?.supportsLongCacheRetention??!0}}function N(e,t){return t==="long"&&e.supportsLongCacheRetention?"24h":void 0}function x(e){if(e instanceof Error){const t=e.status,r=typeof t=="number"?t:void 0;return r!==void 0?`OpenAI API error (${r}): ${e.message}`:e.message}try{return JSON.stringify(e)}catch{return String(e)}}const H=(e,t,r)=>{const s=new I;return(async()=>{const a={role:"assistant",content:[],api:e.api,provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const o=r?.apiKey||h(e.provider)||"",i=g(r?.cacheRetention)==="none"?void 0:r?.sessionId,p=K(e,t,o,r?.headers,i);let c=q(e,t,r);const f=await r?.onPayload?.(c,e);f!==void 0&&(c=f);const l={...r?.signal?{signal:r.signal}:{},...r?.timeoutMs!==void 0?{timeout:r.timeoutMs}:{},...r?.maxRetries!==void 0?{maxRetries:r.maxRetries}:{}},{data:v,response:d}=await p.responses.create(c,l).withResponse();if(await r?.onResponse?.({status:d.status,headers:w(d.headers)},e),s.push({type:"start",partial:a}),await C(v,a,s,e,{serviceTier:r?.serviceTier,applyServiceTierPricing:(R,y)=>W(R,y,e)}),r?.signal?.aborted)throw new Error("Request was aborted");if(a.stopReason==="aborted"||a.stopReason==="error")throw new Error("An unknown error occurred");s.push({type:"done",reason:a.stopReason,message:a}),s.end()}catch(o){for(const n of a.content)delete n.index,delete n.partialJson;a.stopReason=r?.signal?.aborted?"aborted":"error",a.errorMessage=x(o),s.push({type:"error",reason:a.stopReason,error:a}),s.end()}})(),s},Q=(e,t,r)=>{const s=r?.apiKey||h(e.provider);if(!s)throw new Error(`No API key for provider: ${e.provider}`);const a=L(e,r,s),o=r?.reasoning?E(e,r.reasoning):void 0;return H(e,t,{...a,reasoningEffort:o==="off"?void 0:o})};function K(e,t,r,s,a){if(!r){if(!u.OPENAI_API_KEY)throw new Error("OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.");r=u.OPENAI_API_KEY}const o=m(e),n={...e.headers};if(e.provider==="github-copilot"){const p=_(t.messages),c=O({messages:t.messages,hasImages:p});Object.assign(n,c)}a&&(o.sendSessionIdHeader&&(n.session_id=a),n["x-client-request-id"]=a),s&&Object.assign(n,s);const i=e.provider==="cloudflare-ai-gateway"?{...n,Authorization:n.Authorization??null,"cf-aig-authorization":`Bearer ${r}`}:n;return new A({apiKey:r,baseURL:b(e.provider)?P(e):e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:i})}function q(e,t,r){const s=T(e,t,M),a=g(r?.cacheRetention),o=m(e),n={model:e.id,input:s,stream:!0,prompt_cache_key:a==="none"?void 0:S(r?.sessionId),prompt_cache_retention:N(o,a),store:!1};if(r?.maxTokens&&(n.max_output_tokens=r?.maxTokens),r?.temperature!==void 0&&(n.temperature=r?.temperature),r?.serviceTier!==void 0&&(n.service_tier=r.serviceTier),t.tools&&t.tools.length>0&&(n.tools=k(t.tools)),e.reasoning)if(r?.reasoningEffort||r?.reasoningSummary){const i=r?.reasoningEffort?e.thinkingLevelMap?.[r.reasoningEffort]??r.reasoningEffort:"medium";n.reasoning={effort:i,summary:r?.reasoningSummary||"auto"},n.include=["reasoning.encrypted_content"]}else e.provider!=="github-copilot"&&e.thinkingLevelMap?.off!==null&&(n.reasoning={effort:e.thinkingLevelMap?.off??"none"});return n}function B(e,t){switch(t){case"flex":return .5;case"priority":return e.id==="gpt-5.5"?2.5:2;default:return 1}}function W(e,t,r){const s=B(r,t);s!==1&&(e.cost.input*=s,e.cost.output*=s,e.cost.cacheRead*=s,e.cost.cacheWrite*=s,e.cost.total=e.cost.input+e.cost.output+e.cost.cacheRead+e.cost.cacheWrite)}export{H as streamOpenAIResponses,Q as streamSimpleOpenAIResponses};
|
dist/assets/openai-responses-shared-DeOhQuRA.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import{c as S}from"./index-BCicI0Zo.js";import{s as v}from"./hash-DMDecQcg.js";import{a as x}from"./json-parse-B9IFJnx2.js";import{s as _}from"./sanitize-unicode-B62XMGgN.js";import{t as $}from"./transform-messages-Bxe2XUr4.js";function T(r,t){const l={v:1,id:r};return t&&(l.phase=t),JSON.stringify(l)}function C(r){if(r){if(r.startsWith("{"))try{const t=JSON.parse(r);if(t.v===1&&typeof t.id=="string")return t.phase==="commentary"||t.phase==="final_answer"?{id:t.id,phase:t.phase}:{id:t.id}}catch{}return{id:r}}}function M(r,t,l,y){const m=[],i=s=>{const o=s.replace(/[^a-zA-Z0-9_-]/g,"_");return(o.length>64?o.slice(0,64):o).replace(/_+$/,"")},n=s=>{const o=`fc_${v(s)}`;return o.length>64?o.slice(0,64):o},k=(s,o,p)=>{if(!l.has(r.provider)||!s.includes("|"))return i(s);const[h,g]=s.split("|"),f=i(h);let c=p.provider!==r.provider||p.api!==r.api?n(g):i(g);return c.startsWith("fc_")||(c=i(`fc_${c}`)),`${f}|${c}`},d=$(t.messages,r,k);if((y?.includeSystemPrompt??!0)&&t.systemPrompt){const s=r.reasoning?"developer":"system";m.push({role:s,content:_(t.systemPrompt)})}let e=0;for(const s of d){if(s.role==="user")if(typeof s.content=="string")m.push({role:"user",content:[{type:"input_text",text:_(s.content)}]});else{const o=s.content.map(p=>p.type==="text"?{type:"input_text",text:_(p.text)}:{type:"input_image",detail:"auto",image_url:`data:${p.mimeType};base64,${p.data}`});if(o.length===0)continue;m.push({role:"user",content:o})}else if(s.role==="assistant"){const o=[],p=s,h=p.model!==r.id&&p.provider===r.provider&&p.api===r.api;for(const g of s.content)if(g.type==="thinking"){if(g.thinkingSignature){const f=JSON.parse(g.thinkingSignature);o.push(f)}}else if(g.type==="text"){const f=g,u=C(f.textSignature);let c=u?.id;c?c.length>64&&(c=`msg_${v(c)}`):c=`msg_${e}`,o.push({type:"message",role:"assistant",content:[{type:"output_text",text:_(f.text),annotations:[]}],status:"completed",id:c,phase:u?.phase})}else if(g.type==="toolCall"){const f=g,[u,c]=f.id.split("|");let I=c;h&&I?.startsWith("fc_")&&(I=void 0),o.push({type:"function_call",id:I,call_id:u,name:f.name,arguments:JSON.stringify(f.arguments)})}if(o.length===0)continue;m.push(...o)}else if(s.role==="toolResult"){const o=s.content.filter(u=>u.type==="text").map(u=>u.text).join(`
|
| 2 |
+
`),p=s.content.some(u=>u.type==="image"),h=o.length>0,[g]=s.toolCallId.split("|");let f;if(p&&r.input.includes("image")){const u=[];h&&u.push({type:"input_text",text:_(o)});for(const c of s.content)c.type==="image"&&u.push({type:"input_image",detail:"auto",image_url:`data:${c.mimeType};base64,${c.data}`});f=u}else f=_(h?o:"(see attached image)");m.push({type:"function_call_output",call_id:g,output:f})}e++}return m}function W(r,t){const l=t?.strict===void 0?!1:t.strict;return r.map(y=>({type:"function",name:y.name,description:y.description,parameters:y.parameters,strict:l}))}async function N(r,t,l,y,m){let i=null,n=null;const k=t.content,d=()=>k.length-1;for await(const a of r)if(a.type==="response.created")t.responseId=a.response.id;else if(a.type==="response.output_item.added"){const e=a.item;e.type==="reasoning"?(i=e,n={type:"thinking",thinking:""},t.content.push(n),l.push({type:"thinking_start",contentIndex:d(),partial:t})):e.type==="message"?(i=e,n={type:"text",text:""},t.content.push(n),l.push({type:"text_start",contentIndex:d(),partial:t})):e.type==="function_call"&&(i=e,n={type:"toolCall",id:`${e.call_id}|${e.id}`,name:e.name,arguments:{},partialJson:e.arguments||""},t.content.push(n),l.push({type:"toolcall_start",contentIndex:d(),partial:t}))}else if(a.type==="response.reasoning_summary_part.added")i&&i.type==="reasoning"&&(i.summary=i.summary||[],i.summary.push(a.part));else if(a.type==="response.reasoning_summary_text.delta"){if(i?.type==="reasoning"&&n?.type==="thinking"){i.summary=i.summary||[];const e=i.summary[i.summary.length-1];e&&(n.thinking+=a.delta,e.text+=a.delta,l.push({type:"thinking_delta",contentIndex:d(),delta:a.delta,partial:t}))}}else if(a.type==="response.reasoning_summary_part.done"){if(i?.type==="reasoning"&&n?.type==="thinking"){i.summary=i.summary||[];const e=i.summary[i.summary.length-1];e&&(n.thinking+=`
|
| 3 |
+
|
| 4 |
+
`,e.text+=`
|
| 5 |
+
|
| 6 |
+
`,l.push({type:"thinking_delta",contentIndex:d(),delta:`
|
| 7 |
+
|
| 8 |
+
`,partial:t}))}}else if(a.type==="response.reasoning_text.delta")i?.type==="reasoning"&&n?.type==="thinking"&&(n.thinking+=a.delta,l.push({type:"thinking_delta",contentIndex:d(),delta:a.delta,partial:t}));else if(a.type==="response.content_part.added")i?.type==="message"&&(i.content=i.content||[],(a.part.type==="output_text"||a.part.type==="refusal")&&i.content.push(a.part));else if(a.type==="response.output_text.delta"){if(i?.type==="message"&&n?.type==="text"){if(!i.content||i.content.length===0)continue;const e=i.content[i.content.length-1];e?.type==="output_text"&&(n.text+=a.delta,e.text+=a.delta,l.push({type:"text_delta",contentIndex:d(),delta:a.delta,partial:t}))}}else if(a.type==="response.refusal.delta"){if(i?.type==="message"&&n?.type==="text"){if(!i.content||i.content.length===0)continue;const e=i.content[i.content.length-1];e?.type==="refusal"&&(n.text+=a.delta,e.refusal+=a.delta,l.push({type:"text_delta",contentIndex:d(),delta:a.delta,partial:t}))}}else if(a.type==="response.function_call_arguments.delta")i?.type==="function_call"&&n?.type==="toolCall"&&(n.partialJson+=a.delta,n.arguments=x(n.partialJson),l.push({type:"toolcall_delta",contentIndex:d(),delta:a.delta,partial:t}));else if(a.type==="response.function_call_arguments.done"){if(i?.type==="function_call"&&n?.type==="toolCall"){const e=n.partialJson;if(n.partialJson=a.arguments,n.arguments=x(n.partialJson),a.arguments.startsWith(e)){const s=a.arguments.slice(e.length);s.length>0&&l.push({type:"toolcall_delta",contentIndex:d(),delta:s,partial:t})}}}else if(a.type==="response.output_item.done"){const e=a.item;if(e.type==="reasoning"&&n?.type==="thinking"){const s=e.summary?.map(p=>p.text).join(`
|
| 9 |
+
|
| 10 |
+
`)||"",o=e.content?.map(p=>p.text).join(`
|
| 11 |
+
|
| 12 |
+
`)||"";n.thinking=s||o||n.thinking,n.thinkingSignature=JSON.stringify(e),l.push({type:"thinking_end",contentIndex:d(),content:n.thinking,partial:t}),n=null}else if(e.type==="message"&&n?.type==="text")n.text=e.content.map(s=>s.type==="output_text"?s.text:s.refusal).join(""),n.textSignature=T(e.id,e.phase??void 0),l.push({type:"text_end",contentIndex:d(),content:n.text,partial:t}),n=null;else if(e.type==="function_call"){const s=n?.type==="toolCall"&&n.partialJson?x(n.partialJson):x(e.arguments||"{}");let o;n?.type==="toolCall"?(n.arguments=s,delete n.partialJson,o=n):o={type:"toolCall",id:`${e.call_id}|${e.id}`,name:e.name,arguments:s},n=null,l.push({type:"toolcall_end",contentIndex:d(),toolCall:o,partial:t})}}else if(a.type==="response.completed"){const e=a.response;if(e?.id&&(t.responseId=e.id),e?.usage){const s=e.usage.input_tokens_details?.cached_tokens||0;t.usage={input:(e.usage.input_tokens||0)-s,output:e.usage.output_tokens||0,cacheRead:s,cacheWrite:0,totalTokens:e.usage.total_tokens||0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}}}if(S(y,t.usage),m?.applyServiceTierPricing){const s=m.resolveServiceTier?m.resolveServiceTier(e?.service_tier,m.serviceTier):e?.service_tier??m.serviceTier;m.applyServiceTierPricing(t.usage,s)}t.stopReason=J(e?.status),t.content.some(s=>s.type==="toolCall")&&t.stopReason==="stop"&&(t.stopReason="toolUse")}else{if(a.type==="error")throw new Error(`Error Code ${a.code}: ${a.message}`||"Unknown error");if(a.type==="response.failed"){const e=a.response?.error,s=a.response?.incomplete_details,o=e?`${e.code||"unknown"}: ${e.message||"no message"}`:s?.reason?`incomplete: ${s.reason}`:"Unknown error (no error details in response)";throw new Error(o)}}}function J(r){if(!r)return"stop";switch(r){case"completed":return"stop";case"incomplete":return"length";case"failed":case"cancelled":return"error";case"in_progress":case"queued":return"stop";default:{const t=r;throw new Error(`Unhandled stop reason: ${t}`)}}}export{W as a,M as c,N as p};
|
dist/assets/openrouter-C9vLcqev.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
import{O as y}from"./client-Sq9UPDIz.js";import{g as R}from"./index-BCicI0Zo.js";import{h as x}from"./headers-CgnjaPPL.js";import{s as _}from"./sanitize-unicode-B62XMGgN.js";const O=async(e,r,t)=>{const a={api:e.api,provider:e.provider,model:e.id,output:[],stopReason:"stop",timestamp:Date.now()};try{const s=t?.apiKey||R(e.provider);if(!s)throw new Error(`No API key available for provider: ${e.provider}`);const c=k(e,s,t?.headers);let n=w(e,r);const i=await t?.onPayload?.(n,e);i!==void 0&&(n=i);const o={...t?.signal?{signal:t.signal}:{},...t?.timeoutMs!==void 0?{timeout:t.timeoutMs}:{},...t?.maxRetries!==void 0?{maxRetries:t.maxRetries}:{}},{data:l,response:h}=await c.chat.completions.create(n,o).withResponse();await t?.onResponse?.({status:h.status,headers:x(h.headers)},e);const u=l;a.responseId=u.id,u.usage&&(a.usage=b(u.usage,e));const p=u.choices[0];if(p){const m=p.message.content;typeof m=="string"&&m.length>0&&a.output.push({type:"text",text:m});for(const d of p.message.images??[]){const f=typeof d.image_url=="string"?d.image_url:d.image_url?.url;if(!f?.startsWith("data:"))continue;const g=f.match(/^data:([^;]+);base64,(.+)$/);g&&a.output.push({type:"image",mimeType:g[1],data:g[2]})}}return a}catch(s){return a.stopReason=t?.signal?.aborted?"aborted":"error",a.errorMessage=s instanceof Error?s.message:JSON.stringify(s),a}};function k(e,r,t){return new y({apiKey:r,baseURL:e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:{...e.headers,...t}})}function w(e,r){const t=r.input.map(a=>a.type==="text"?{type:"text",text:_(a.text)}:{type:"image_url",image_url:{url:`data:${a.mimeType};base64,${a.data}`}});return{model:e.id,messages:[{role:"user",content:t}],stream:!1,modalities:e.output.includes("text")?["image","text"]:["image"]}}function b(e,r){const t=e.prompt_tokens||0,a=e.prompt_tokens_details?.cached_tokens||0,s=e.prompt_tokens_details?.cache_write_tokens||0,c=s>0?Math.max(0,a-s):a,n=Math.max(0,t-c-s),i=e.completion_tokens||0,o={input:n,output:i,cacheRead:c,cacheWrite:s,totalTokens:n+i+c+s,cost:{input:r.cost.input/1e6*n,output:r.cost.output/1e6*i,cacheRead:r.cost.cacheRead/1e6*c,cacheWrite:r.cost.cacheWrite/1e6*s,total:0}};return o.cost.total=o.cost.input+o.cost.output+o.cost.cacheRead+o.cost.cacheWrite,o}export{O as generateImagesOpenRouter};
|
dist/assets/ort-wasm-simd-threaded.asyncify-DMmc6YqF.wasm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e0c0c6d3e73d43b8a249972f8358f845b08cc16fec3c80efafdf8bed40366786
|
| 3 |
+
size 23567050
|
dist/assets/sanitize-unicode-B62XMGgN.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
function F(u){return u.replace(new RegExp("[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?<![\\uD800-\\uDBFF])[\\uDC00-\\uDFFF]","g"),"")}export{F as s};
|
dist/assets/transform-messages-Bxe2XUr4.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
function S(o,t,n){return{temperature:t?.temperature,maxTokens:t?.maxTokens,signal:t?.signal,apiKey:n||t?.apiKey,transport:t?.transport,cacheRetention:t?.cacheRetention,sessionId:t?.sessionId,headers:t?.headers,onPayload:t?.onPayload,onResponse:t?.onResponse,timeoutMs:t?.timeoutMs,maxRetries:t?.maxRetries,maxRetryDelayMs:t?.maxRetryDelayMs,metadata:t?.metadata}}function R(o){return o==="xhigh"?"high":o}function O(o,t,n,l){const m={...{minimal:1024,low:2048,medium:8192,high:16384},...l},u=1024,f=R(n);let s=m[f];const d=o===void 0?t:Math.min(o+s,t);return d<=s&&(s=Math.max(0,d-u)),{maxTokens:d,thinkingBudget:s}}const I="(image omitted: model does not support images)",M="(tool image omitted: model does not support images)";function y(o,t){const n=[];let l=!1;for(const h of o){if(h.type==="image"){l||n.push({type:"text",text:t}),l=!0;continue}n.push(h),l=h.text===t}return n}function C(o,t){return t.input.includes("image")?o:o.map(n=>n.role==="user"&&Array.isArray(n.content)?{...n,content:y(n.content,I)}:n.role==="toolResult"?{...n,content:y(n.content,M)}:n)}function w(o,t,n){const l=new Map,m=C(o,t).map(r=>{if(r.role==="user")return r;if(r.role==="toolResult"){const e=l.get(r.toolCallId);return e&&e!==r.toolCallId?{...r,toolCallId:e}:r}if(r.role==="assistant"){const e=r,i=e.provider===t.provider&&e.api===t.api&&e.model===t.id,g=e.content.flatMap(a=>{if(a.type==="thinking")return a.redacted?i?a:[]:i&&a.thinkingSignature?a:!a.thinking||a.thinking.trim()===""?[]:i?a:{type:"text",text:a.thinking};if(a.type==="text")return i?a:{type:"text",text:a.text};if(a.type==="toolCall"){const c=a;let p=c;if(!i&&c.thoughtSignature&&(p={...c},delete p.thoughtSignature),!i&&n){const x=n(c.id,t,e);x!==c.id&&(l.set(c.id,x),p={...p,id:x})}return p}return a});return{...e,content:g}}return r}),u=[];let f=[],s=new Set;const d=()=>{if(f.length>0){for(const r of f)s.has(r.id)||u.push({role:"toolResult",toolCallId:r.id,toolName:r.name,content:[{type:"text",text:"No result provided"}],isError:!0,timestamp:Date.now()});f=[],s=new Set}};for(let r=0;r<m.length;r++){const e=m[r];if(e.role==="assistant"){d();const i=e;if(i.stopReason==="error"||i.stopReason==="aborted")continue;const g=i.content.filter(a=>a.type==="toolCall");g.length>0&&(f=g,s=new Set),u.push(e)}else e.role==="toolResult"?(s.add(e.toolCallId),u.push(e)):(e.role==="user"&&d(),u.push(e))}return d(),u}export{O as a,S as b,w as t};
|
dist/index.html
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>MiniCPM5 Pi Web Agent</title>
|
| 7 |
+
<script type="module" crossorigin src="/assets/index-BCicI0Zo.js"></script>
|
| 8 |
+
<link rel="stylesheet" crossorigin href="/assets/index-CRS_ZGq-.css">
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<main id="app">
|
| 12 |
+
<section class="shell">
|
| 13 |
+
<header class="topbar">
|
| 14 |
+
<div>
|
| 15 |
+
<h1>Pi Web Agent</h1>
|
| 16 |
+
<p id="model-label"></p>
|
| 17 |
+
</div>
|
| 18 |
+
<div class="status-stack" aria-label="Runtime status">
|
| 19 |
+
<span class="status" id="status">Idle</span>
|
| 20 |
+
<span class="status" id="model-status">Model idle</span>
|
| 21 |
+
<span class="status" id="sandbox-status">Sandbox idle</span>
|
| 22 |
+
</div>
|
| 23 |
+
</header>
|
| 24 |
+
|
| 25 |
+
<section class="controls" aria-label="Agent controls">
|
| 26 |
+
<label>
|
| 27 |
+
Model
|
| 28 |
+
<select id="mode">
|
| 29 |
+
<option value="local">MiniCPM5 q4</option>
|
| 30 |
+
<option value="mock">Deterministic test</option>
|
| 31 |
+
</select>
|
| 32 |
+
</label>
|
| 33 |
+
<label>
|
| 34 |
+
Device
|
| 35 |
+
<select id="device">
|
| 36 |
+
<option value="webgpu">WebGPU</option>
|
| 37 |
+
<option value="wasm">WASM</option>
|
| 38 |
+
</select>
|
| 39 |
+
</label>
|
| 40 |
+
<label>
|
| 41 |
+
Max tokens
|
| 42 |
+
<input id="max-new-tokens" type="number" min="16" max="512" step="1" value="160" />
|
| 43 |
+
</label>
|
| 44 |
+
<label>
|
| 45 |
+
Temperature
|
| 46 |
+
<input id="temperature" type="number" min="0" max="1.5" step="0.05" value="0" />
|
| 47 |
+
</label>
|
| 48 |
+
</section>
|
| 49 |
+
|
| 50 |
+
<section class="command-row" aria-label="Sandbox actions">
|
| 51 |
+
<button id="boot-sandbox" type="button">Boot Sandbox</button>
|
| 52 |
+
<button id="reset-sandbox" type="button">Reset</button>
|
| 53 |
+
<button id="demo-prompt" type="button">Demo Prompt</button>
|
| 54 |
+
</section>
|
| 55 |
+
|
| 56 |
+
<section class="workspace">
|
| 57 |
+
<div class="prompt-pane">
|
| 58 |
+
<label class="prompt-label" for="prompt">Task</label>
|
| 59 |
+
<textarea id="prompt" spellcheck="true">Create hello.js that prints the result of 21 * 2, run it with Node, and tell me the command output.</textarea>
|
| 60 |
+
<button id="run" type="button">Run Agent</button>
|
| 61 |
+
</div>
|
| 62 |
+
|
| 63 |
+
<div class="panes">
|
| 64 |
+
<section class="pane">
|
| 65 |
+
<h2>Transcript</h2>
|
| 66 |
+
<pre id="transcript"></pre>
|
| 67 |
+
</section>
|
| 68 |
+
<section class="pane">
|
| 69 |
+
<h2>Sandbox Files</h2>
|
| 70 |
+
<pre id="files"></pre>
|
| 71 |
+
</section>
|
| 72 |
+
<section class="pane wide">
|
| 73 |
+
<h2>Events</h2>
|
| 74 |
+
<pre id="event-log"></pre>
|
| 75 |
+
</section>
|
| 76 |
+
</div>
|
| 77 |
+
</section>
|
| 78 |
+
</section>
|
| 79 |
+
</main>
|
| 80 |
+
</body>
|
| 81 |
+
</html>
|
| 82 |
+
|
index.html
CHANGED
|
@@ -1,19 +1,81 @@
|
|
| 1 |
<!doctype html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
|
|
| 1 |
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>MiniCPM5 Pi Web Agent</title>
|
| 7 |
+
</head>
|
| 8 |
+
<body>
|
| 9 |
+
<main id="app">
|
| 10 |
+
<section class="shell">
|
| 11 |
+
<header class="topbar">
|
| 12 |
+
<div>
|
| 13 |
+
<h1>Pi Web Agent</h1>
|
| 14 |
+
<p id="model-label"></p>
|
| 15 |
+
</div>
|
| 16 |
+
<div class="status-stack" aria-label="Runtime status">
|
| 17 |
+
<span class="status" id="status">Idle</span>
|
| 18 |
+
<span class="status" id="model-status">Model idle</span>
|
| 19 |
+
<span class="status" id="sandbox-status">Sandbox idle</span>
|
| 20 |
+
</div>
|
| 21 |
+
</header>
|
| 22 |
+
|
| 23 |
+
<section class="controls" aria-label="Agent controls">
|
| 24 |
+
<label>
|
| 25 |
+
Model
|
| 26 |
+
<select id="mode">
|
| 27 |
+
<option value="local">MiniCPM5 q4</option>
|
| 28 |
+
<option value="mock">Deterministic test</option>
|
| 29 |
+
</select>
|
| 30 |
+
</label>
|
| 31 |
+
<label>
|
| 32 |
+
Device
|
| 33 |
+
<select id="device">
|
| 34 |
+
<option value="webgpu">WebGPU</option>
|
| 35 |
+
<option value="wasm">WASM</option>
|
| 36 |
+
</select>
|
| 37 |
+
</label>
|
| 38 |
+
<label>
|
| 39 |
+
Max tokens
|
| 40 |
+
<input id="max-new-tokens" type="number" min="16" max="512" step="1" value="160" />
|
| 41 |
+
</label>
|
| 42 |
+
<label>
|
| 43 |
+
Temperature
|
| 44 |
+
<input id="temperature" type="number" min="0" max="1.5" step="0.05" value="0" />
|
| 45 |
+
</label>
|
| 46 |
+
</section>
|
| 47 |
+
|
| 48 |
+
<section class="command-row" aria-label="Sandbox actions">
|
| 49 |
+
<button id="boot-sandbox" type="button">Boot Sandbox</button>
|
| 50 |
+
<button id="reset-sandbox" type="button">Reset</button>
|
| 51 |
+
<button id="demo-prompt" type="button">Demo Prompt</button>
|
| 52 |
+
</section>
|
| 53 |
+
|
| 54 |
+
<section class="workspace">
|
| 55 |
+
<div class="prompt-pane">
|
| 56 |
+
<label class="prompt-label" for="prompt">Task</label>
|
| 57 |
+
<textarea id="prompt" spellcheck="true">Create hello.js that prints the result of 21 * 2, run it with Node, and tell me the command output.</textarea>
|
| 58 |
+
<button id="run" type="button">Run Agent</button>
|
| 59 |
+
</div>
|
| 60 |
+
|
| 61 |
+
<div class="panes">
|
| 62 |
+
<section class="pane">
|
| 63 |
+
<h2>Transcript</h2>
|
| 64 |
+
<pre id="transcript"></pre>
|
| 65 |
+
</section>
|
| 66 |
+
<section class="pane">
|
| 67 |
+
<h2>Sandbox Files</h2>
|
| 68 |
+
<pre id="files"></pre>
|
| 69 |
+
</section>
|
| 70 |
+
<section class="pane wide">
|
| 71 |
+
<h2>Events</h2>
|
| 72 |
+
<pre id="event-log"></pre>
|
| 73 |
+
</section>
|
| 74 |
+
</div>
|
| 75 |
+
</section>
|
| 76 |
+
</section>
|
| 77 |
+
</main>
|
| 78 |
+
<script type="module" src="/src/main.js"></script>
|
| 79 |
+
</body>
|
| 80 |
</html>
|
| 81 |
+
|
package-lock.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "minicpm5-transformersjs-web",
|
| 3 |
+
"private": true,
|
| 4 |
+
"type": "module",
|
| 5 |
+
"scripts": {
|
| 6 |
+
"dev": "vite --host 0.0.0.0",
|
| 7 |
+
"build": "vite build",
|
| 8 |
+
"preview": "vite preview --host 0.0.0.0",
|
| 9 |
+
"smoke:local-model": "node scripts/smoke_local_model_web_agent.mjs",
|
| 10 |
+
"smoke:web": "node scripts/smoke_web_agent.mjs",
|
| 11 |
+
"verify": "node scripts/verify_tjs_model.mjs"
|
| 12 |
+
},
|
| 13 |
+
"dependencies": {
|
| 14 |
+
"@earendil-works/pi-agent-core": "^0.75.5",
|
| 15 |
+
"@earendil-works/pi-ai": "^0.75.5",
|
| 16 |
+
"@huggingface/transformers": "^4.2.0",
|
| 17 |
+
"@webcontainer/api": "^1.6.4",
|
| 18 |
+
"vite": "^7.2.0"
|
| 19 |
+
},
|
| 20 |
+
"devDependencies": {
|
| 21 |
+
"@playwright/test": "^1.60.0"
|
| 22 |
+
}
|
| 23 |
+
}
|
scripts/convert_minicpm5_tjs.py
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Convert openbmb/MiniCPM5-1B to a Transformers.js q4 ONNX repo."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import argparse
|
| 7 |
+
import json
|
| 8 |
+
import logging
|
| 9 |
+
import os
|
| 10 |
+
import shutil
|
| 11 |
+
import subprocess
|
| 12 |
+
import sys
|
| 13 |
+
import tarfile
|
| 14 |
+
import tempfile
|
| 15 |
+
import urllib.request
|
| 16 |
+
from dataclasses import asdict
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
from huggingface_hub import HfApi, create_repo
|
| 20 |
+
from optimum.exporters.onnx import main_export
|
| 21 |
+
from transformers import AutoConfig
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
SOURCE_MODEL = "openbmb/MiniCPM5-1B"
|
| 25 |
+
TRANSFORMERS_JS_TAG = "3.8.1"
|
| 26 |
+
TRANSFORMERS_JS_TARBALL = (
|
| 27 |
+
f"https://github.com/huggingface/transformers.js/archive/refs/tags/{TRANSFORMERS_JS_TAG}.tar.gz"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def run(cmd: list[str], cwd: Path | None = None) -> None:
|
| 32 |
+
print("+", " ".join(cmd), flush=True)
|
| 33 |
+
subprocess.run(cmd, cwd=cwd, check=True)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def download_transformers_js(work_dir: Path) -> Path:
|
| 37 |
+
archive_path = work_dir / "transformers.js.tar.gz"
|
| 38 |
+
urllib.request.urlretrieve(TRANSFORMERS_JS_TARBALL, archive_path)
|
| 39 |
+
with tarfile.open(archive_path) as archive:
|
| 40 |
+
archive.extractall(work_dir)
|
| 41 |
+
return work_dir / f"transformers.js-{TRANSFORMERS_JS_TAG}"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def patch_config(config_path: Path, dtype: str, q4_external_chunks: int) -> None:
|
| 45 |
+
config = json.loads(config_path.read_text())
|
| 46 |
+
config.setdefault("transformers.js_config", {})
|
| 47 |
+
config["transformers.js_config"]["dtype"] = dtype
|
| 48 |
+
if q4_external_chunks:
|
| 49 |
+
config["transformers.js_config"]["use_external_data_format"] = {
|
| 50 |
+
"model_q4.onnx": q4_external_chunks,
|
| 51 |
+
}
|
| 52 |
+
else:
|
| 53 |
+
config["transformers.js_config"].pop("use_external_data_format", None)
|
| 54 |
+
config_path.write_text(json.dumps(config, indent=2) + "\n")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def write_readme(output_dir: Path, source_model: str, target_repo: str) -> None:
|
| 58 |
+
readme = f"""---
|
| 59 |
+
license: apache-2.0
|
| 60 |
+
library_name: transformers.js
|
| 61 |
+
pipeline_tag: text-generation
|
| 62 |
+
base_model: {source_model}
|
| 63 |
+
tags:
|
| 64 |
+
- transformers.js
|
| 65 |
+
- onnx
|
| 66 |
+
- onnxruntime-web
|
| 67 |
+
- llama
|
| 68 |
+
- minicpm5
|
| 69 |
+
- text-generation
|
| 70 |
+
- browser
|
| 71 |
+
- webgpu
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
# MiniCPM5-1B ONNX Web
|
| 75 |
+
|
| 76 |
+
Transformers.js q4 ONNX export of `{source_model}` for browser text generation.
|
| 77 |
+
|
| 78 |
+
## Files
|
| 79 |
+
|
| 80 |
+
- `onnx/model_q4.onnx`: ONNX Runtime 4-bit MatMul quantized decoder with KV cache.
|
| 81 |
+
- `config.json`: includes `transformers.js_config.dtype = "q4"` so Transformers.js loads the q4 artifact by default.
|
| 82 |
+
- tokenizer and generation config files copied from the source model export.
|
| 83 |
+
|
| 84 |
+
## Usage
|
| 85 |
+
|
| 86 |
+
```js
|
| 87 |
+
import {{ pipeline }} from "@huggingface/transformers";
|
| 88 |
+
|
| 89 |
+
const generator = await pipeline("text-generation", "{target_repo}", {{
|
| 90 |
+
dtype: "q4",
|
| 91 |
+
device: "webgpu",
|
| 92 |
+
}});
|
| 93 |
+
|
| 94 |
+
const output = await generator("Briefly introduce yourself.", {{
|
| 95 |
+
max_new_tokens: 64,
|
| 96 |
+
temperature: 0.2,
|
| 97 |
+
do_sample: true,
|
| 98 |
+
}});
|
| 99 |
+
console.log(output[0].generated_text);
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
If WebGPU is unavailable, use `device: "wasm"` in the browser.
|
| 103 |
+
"""
|
| 104 |
+
(output_dir / "README.md").write_text(readme)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def convert(args: argparse.Namespace) -> Path:
|
| 108 |
+
work_dir = Path(args.work_dir or tempfile.mkdtemp(prefix="minicpm5-tjs-")).resolve()
|
| 109 |
+
work_dir.mkdir(parents=True, exist_ok=True)
|
| 110 |
+
print(f"Working directory: {work_dir}", flush=True)
|
| 111 |
+
|
| 112 |
+
transformers_js_dir = download_transformers_js(work_dir)
|
| 113 |
+
scripts_dir = transformers_js_dir / "scripts"
|
| 114 |
+
sys.path.insert(0, str(transformers_js_dir))
|
| 115 |
+
|
| 116 |
+
from scripts.quantize import QuantizationArguments, quantize
|
| 117 |
+
logging.getLogger("onnxruntime.quantization.matmul_4bits_quantizer").setLevel(logging.WARNING)
|
| 118 |
+
|
| 119 |
+
export_root = work_dir / "export"
|
| 120 |
+
model_dir = export_root / args.source_model
|
| 121 |
+
model_dir.mkdir(parents=True, exist_ok=True)
|
| 122 |
+
|
| 123 |
+
device = args.device
|
| 124 |
+
if device == "auto":
|
| 125 |
+
try:
|
| 126 |
+
import torch
|
| 127 |
+
|
| 128 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 129 |
+
except Exception:
|
| 130 |
+
device = "cpu"
|
| 131 |
+
print(f"Export device: {device}", flush=True)
|
| 132 |
+
|
| 133 |
+
config = AutoConfig.from_pretrained(args.source_model)
|
| 134 |
+
print(
|
| 135 |
+
"Source config:",
|
| 136 |
+
json.dumps(
|
| 137 |
+
{
|
| 138 |
+
"model_type": config.model_type,
|
| 139 |
+
"architectures": getattr(config, "architectures", None),
|
| 140 |
+
"hidden_size": getattr(config, "hidden_size", None),
|
| 141 |
+
"num_hidden_layers": getattr(config, "num_hidden_layers", None),
|
| 142 |
+
"torch_dtype": str(getattr(config, "torch_dtype", None)),
|
| 143 |
+
},
|
| 144 |
+
indent=2,
|
| 145 |
+
),
|
| 146 |
+
flush=True,
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
main_export(
|
| 150 |
+
model_name_or_path=args.source_model,
|
| 151 |
+
output=model_dir,
|
| 152 |
+
task="text-generation-with-past",
|
| 153 |
+
opset=args.opset,
|
| 154 |
+
device=device,
|
| 155 |
+
dtype=args.export_dtype,
|
| 156 |
+
do_validation=False,
|
| 157 |
+
trust_remote_code=False,
|
| 158 |
+
library_name="transformers",
|
| 159 |
+
slim=False,
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
onnx_dir = model_dir / "onnx"
|
| 163 |
+
onnx_dir.mkdir(exist_ok=True)
|
| 164 |
+
|
| 165 |
+
quant_args = QuantizationArguments(
|
| 166 |
+
modes=["q4"],
|
| 167 |
+
per_channel=False,
|
| 168 |
+
reduce_range=False,
|
| 169 |
+
block_size=args.block_size,
|
| 170 |
+
is_symmetric=True,
|
| 171 |
+
accuracy_level=None,
|
| 172 |
+
op_block_list=None,
|
| 173 |
+
)
|
| 174 |
+
quantize(str(model_dir), str(onnx_dir), quant_args)
|
| 175 |
+
(model_dir / "quantize_config.json").write_text(json.dumps(asdict(quant_args), indent=2) + "\n")
|
| 176 |
+
|
| 177 |
+
for path in model_dir.glob("*.onnx*"):
|
| 178 |
+
path.unlink()
|
| 179 |
+
|
| 180 |
+
q4_model = onnx_dir / "model_q4.onnx"
|
| 181 |
+
if not q4_model.exists():
|
| 182 |
+
raise FileNotFoundError(f"Missing expected quantized model: {q4_model}")
|
| 183 |
+
|
| 184 |
+
q4_external_chunks = 1 if (onnx_dir / "model_q4.onnx_data").exists() else 0
|
| 185 |
+
patch_config(model_dir / "config.json", "q4", q4_external_chunks)
|
| 186 |
+
write_readme(model_dir, args.source_model, args.target_repo)
|
| 187 |
+
|
| 188 |
+
if args.output_dir:
|
| 189 |
+
final_dir = Path(args.output_dir).resolve()
|
| 190 |
+
if final_dir.exists():
|
| 191 |
+
shutil.rmtree(final_dir)
|
| 192 |
+
shutil.copytree(model_dir, final_dir)
|
| 193 |
+
model_dir = final_dir
|
| 194 |
+
|
| 195 |
+
print("Final files:", flush=True)
|
| 196 |
+
for file in sorted(p.relative_to(model_dir).as_posix() for p in model_dir.rglob("*") if p.is_file()):
|
| 197 |
+
print(file, flush=True)
|
| 198 |
+
|
| 199 |
+
if args.target_repo:
|
| 200 |
+
token = os.environ.get("HF_TOKEN") or True
|
| 201 |
+
create_repo(args.target_repo, repo_type="model", private=args.private, exist_ok=True, token=token)
|
| 202 |
+
api = HfApi(token=token)
|
| 203 |
+
api.upload_folder(
|
| 204 |
+
repo_id=args.target_repo,
|
| 205 |
+
repo_type="model",
|
| 206 |
+
folder_path=str(model_dir),
|
| 207 |
+
commit_message=f"Add q4 Transformers.js export of {args.source_model}",
|
| 208 |
+
)
|
| 209 |
+
print(f"Uploaded to https://huggingface.co/{args.target_repo}", flush=True)
|
| 210 |
+
|
| 211 |
+
return model_dir
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def parse_args() -> argparse.Namespace:
|
| 215 |
+
parser = argparse.ArgumentParser()
|
| 216 |
+
parser.add_argument("--source-model", default=SOURCE_MODEL)
|
| 217 |
+
parser.add_argument("--target-repo", default="")
|
| 218 |
+
parser.add_argument("--output-dir", default="")
|
| 219 |
+
parser.add_argument("--work-dir", default="")
|
| 220 |
+
parser.add_argument("--device", default="auto", choices=["auto", "cpu", "cuda"])
|
| 221 |
+
parser.add_argument("--export-dtype", default="fp32", choices=["fp32", "fp16"])
|
| 222 |
+
parser.add_argument("--opset", type=int, default=18)
|
| 223 |
+
parser.add_argument("--block-size", type=int, default=32)
|
| 224 |
+
parser.add_argument("--private", action="store_true")
|
| 225 |
+
return parser.parse_args()
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
if __name__ == "__main__":
|
| 229 |
+
convert(parse_args())
|
scripts/smoke_local_model_web_agent.mjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { chromium } from "@playwright/test";
|
| 2 |
+
|
| 3 |
+
const baseUrl = process.argv[2] || "http://localhost:5173/?device=wasm";
|
| 4 |
+
const executablePath = process.env.CHROMIUM_PATH || "/snap/bin/chromium";
|
| 5 |
+
|
| 6 |
+
const browser = await chromium.launch({
|
| 7 |
+
executablePath,
|
| 8 |
+
headless: true,
|
| 9 |
+
args: ["--no-sandbox", "--disable-dev-shm-usage"],
|
| 10 |
+
});
|
| 11 |
+
|
| 12 |
+
try {
|
| 13 |
+
const page = await browser.newPage();
|
| 14 |
+
page.setDefaultTimeout(600000);
|
| 15 |
+
const consoleLines = [];
|
| 16 |
+
page.on("console", (message) => {
|
| 17 |
+
consoleLines.push(`${message.type()}: ${message.text()}`);
|
| 18 |
+
});
|
| 19 |
+
page.on("pageerror", (error) => {
|
| 20 |
+
consoleLines.push(`pageerror: ${error.stack || error.message}`);
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
await page.goto(baseUrl, { waitUntil: "networkidle" });
|
| 24 |
+
await page.waitForFunction(() => document.querySelector("#status")?.textContent === "Ready");
|
| 25 |
+
|
| 26 |
+
const isolated = await page.evaluate(() => globalThis.crossOriginIsolated);
|
| 27 |
+
if (!isolated) throw new Error("Page is not cross-origin isolated.");
|
| 28 |
+
|
| 29 |
+
await page.fill("#max-new-tokens", "1");
|
| 30 |
+
await page.fill("#temperature", "0");
|
| 31 |
+
await page.click("#run");
|
| 32 |
+
await page.waitForFunction(() => document.querySelector("#status")?.textContent === "Agent running", null, {
|
| 33 |
+
timeout: 10000,
|
| 34 |
+
});
|
| 35 |
+
await page.waitForFunction(() => document.querySelector("#status")?.textContent === "Ready", null, {
|
| 36 |
+
timeout: 600000,
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
const transcript = await page.textContent("#transcript");
|
| 40 |
+
const files = await page.textContent("#files");
|
| 41 |
+
const events = await page.textContent("#event-log");
|
| 42 |
+
const modelStatus = await page.textContent("#model-status");
|
| 43 |
+
|
| 44 |
+
if (!transcript?.includes("pi sandbox result: 42")) {
|
| 45 |
+
throw new Error(`Expected command output in transcript.\n\nTranscript:\n${transcript}\n\nEvents:\n${events}`);
|
| 46 |
+
}
|
| 47 |
+
if (!files?.includes("hello.js")) {
|
| 48 |
+
throw new Error(`Expected hello.js in file listing.\n\nFiles:\n${files}`);
|
| 49 |
+
}
|
| 50 |
+
if (modelStatus !== "Model ready") {
|
| 51 |
+
throw new Error(`Expected MiniCPM model status to be ready, got: ${modelStatus}`);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
console.log(JSON.stringify({ ok: true, isolated, modelStatus, transcript, files }, null, 2));
|
| 55 |
+
} catch (error) {
|
| 56 |
+
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
| 57 |
+
throw error;
|
| 58 |
+
} finally {
|
| 59 |
+
await browser.close();
|
| 60 |
+
}
|
| 61 |
+
|
scripts/smoke_web_agent.mjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { chromium } from "@playwright/test";
|
| 2 |
+
|
| 3 |
+
const baseUrl = process.argv[2] || "http://localhost:5173/?mode=mock&device=wasm";
|
| 4 |
+
const executablePath = process.env.CHROMIUM_PATH || "/snap/bin/chromium";
|
| 5 |
+
|
| 6 |
+
const browser = await chromium.launch({
|
| 7 |
+
executablePath,
|
| 8 |
+
headless: true,
|
| 9 |
+
args: ["--no-sandbox", "--disable-dev-shm-usage"],
|
| 10 |
+
});
|
| 11 |
+
|
| 12 |
+
try {
|
| 13 |
+
const page = await browser.newPage();
|
| 14 |
+
page.setDefaultTimeout(90000);
|
| 15 |
+
|
| 16 |
+
const consoleLines = [];
|
| 17 |
+
page.on("console", (message) => {
|
| 18 |
+
consoleLines.push(`${message.type()}: ${message.text()}`);
|
| 19 |
+
});
|
| 20 |
+
page.on("pageerror", (error) => {
|
| 21 |
+
consoleLines.push(`pageerror: ${error.stack || error.message}`);
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
await page.goto(baseUrl, { waitUntil: "domcontentloaded" });
|
| 25 |
+
const isolated = await page.evaluate(() => globalThis.crossOriginIsolated);
|
| 26 |
+
if (!isolated) {
|
| 27 |
+
throw new Error("Page is not cross-origin isolated.");
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
await page.click("#boot-sandbox");
|
| 31 |
+
await page.waitForFunction(() => document.querySelector("#sandbox-status")?.textContent === "Sandbox ready");
|
| 32 |
+
await page.waitForFunction(() => document.querySelector("#files")?.textContent?.includes("hello.js"));
|
| 33 |
+
|
| 34 |
+
await page.click("#run");
|
| 35 |
+
await page.waitForFunction(() => document.querySelector("#status")?.textContent === "Ready", null, {
|
| 36 |
+
timeout: 120000,
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
const transcript = await page.textContent("#transcript");
|
| 40 |
+
const files = await page.textContent("#files");
|
| 41 |
+
const events = await page.textContent("#event-log");
|
| 42 |
+
|
| 43 |
+
if (!transcript?.includes("pi sandbox result: 42")) {
|
| 44 |
+
throw new Error(`Expected command output in transcript.\n\nTranscript:\n${transcript}\n\nEvents:\n${events}`);
|
| 45 |
+
}
|
| 46 |
+
if (!files?.includes("hello.js")) {
|
| 47 |
+
throw new Error(`Expected hello.js in file listing.\n\nFiles:\n${files}`);
|
| 48 |
+
}
|
| 49 |
+
if (!events?.includes("run_command finished")) {
|
| 50 |
+
throw new Error(`Expected pi tool execution events.\n\nEvents:\n${events}`);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
console.log(JSON.stringify({ ok: true, isolated, transcript, files }, null, 2));
|
| 54 |
+
} catch (error) {
|
| 55 |
+
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
| 56 |
+
throw error;
|
| 57 |
+
} finally {
|
| 58 |
+
await browser.close();
|
| 59 |
+
}
|
| 60 |
+
|
scripts/verify_tjs_model.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { env, ModelRegistry, pipeline } from "@huggingface/transformers";
|
| 2 |
+
|
| 3 |
+
const modelId = process.argv[2] || "Mike0021/MiniCPM5-1B-ONNX-Web";
|
| 4 |
+
const prompt = process.argv[3] || "Hello";
|
| 5 |
+
|
| 6 |
+
env.allowLocalModels = true;
|
| 7 |
+
env.allowRemoteModels = true;
|
| 8 |
+
|
| 9 |
+
const files = await ModelRegistry.get_pipeline_files("text-generation", modelId, {
|
| 10 |
+
dtype: "q4",
|
| 11 |
+
device: "cpu",
|
| 12 |
+
});
|
| 13 |
+
console.log("files", JSON.stringify(files, null, 2));
|
| 14 |
+
|
| 15 |
+
if (!files.includes("onnx/model_q4.onnx")) {
|
| 16 |
+
throw new Error("Expected onnx/model_q4.onnx in Transformers.js file plan.");
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
const generator = await pipeline("text-generation", modelId, {
|
| 20 |
+
dtype: "q4",
|
| 21 |
+
device: "cpu",
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
const result = await generator(prompt, {
|
| 25 |
+
max_new_tokens: 2,
|
| 26 |
+
do_sample: false,
|
| 27 |
+
return_full_text: false,
|
| 28 |
+
});
|
| 29 |
+
|
| 30 |
+
console.log("generation", JSON.stringify(result, null, 2));
|
| 31 |
+
|
src/main.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { MODEL_ID, createPiAgent } from "./piAgent.js";
|
| 2 |
+
import { createSandbox } from "./sandbox.js";
|
| 3 |
+
import "./styles.css";
|
| 4 |
+
|
| 5 |
+
const nodes = {
|
| 6 |
+
status: document.querySelector("#status"),
|
| 7 |
+
modelStatus: document.querySelector("#model-status"),
|
| 8 |
+
sandboxStatus: document.querySelector("#sandbox-status"),
|
| 9 |
+
transcript: document.querySelector("#transcript"),
|
| 10 |
+
eventLog: document.querySelector("#event-log"),
|
| 11 |
+
files: document.querySelector("#files"),
|
| 12 |
+
prompt: document.querySelector("#prompt"),
|
| 13 |
+
run: document.querySelector("#run"),
|
| 14 |
+
boot: document.querySelector("#boot-sandbox"),
|
| 15 |
+
reset: document.querySelector("#reset-sandbox"),
|
| 16 |
+
demo: document.querySelector("#demo-prompt"),
|
| 17 |
+
mode: document.querySelector("#mode"),
|
| 18 |
+
device: document.querySelector("#device"),
|
| 19 |
+
maxTokens: document.querySelector("#max-new-tokens"),
|
| 20 |
+
temperature: document.querySelector("#temperature"),
|
| 21 |
+
modelLabel: document.querySelector("#model-label"),
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
nodes.modelLabel.textContent = MODEL_ID;
|
| 25 |
+
if (!navigator.gpu) nodes.device.value = "wasm";
|
| 26 |
+
|
| 27 |
+
const params = new URLSearchParams(window.location.search);
|
| 28 |
+
if (params.get("mode") === "mock") nodes.mode.value = "mock";
|
| 29 |
+
if (params.get("device")) nodes.device.value = params.get("device");
|
| 30 |
+
|
| 31 |
+
const sandbox = createSandbox({
|
| 32 |
+
onStatus: (text) => setSandboxStatus(text),
|
| 33 |
+
onLog: (text) => logEvent("sandbox", text),
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
let agent = createAgent();
|
| 37 |
+
|
| 38 |
+
function textFromContent(content) {
|
| 39 |
+
if (typeof content === "string") return content;
|
| 40 |
+
if (!Array.isArray(content)) return "";
|
| 41 |
+
return content
|
| 42 |
+
.filter((part) => part.type === "text")
|
| 43 |
+
.map((part) => part.text)
|
| 44 |
+
.join("\n");
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
function setStatus(text) {
|
| 48 |
+
nodes.status.textContent = text;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
function setSandboxStatus(text) {
|
| 52 |
+
nodes.sandboxStatus.textContent = text;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
function setModelStatus(text) {
|
| 56 |
+
nodes.modelStatus.textContent = text;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
function logEvent(kind, text) {
|
| 60 |
+
const line = `[${new Date().toLocaleTimeString()}] ${kind}: ${text}`;
|
| 61 |
+
nodes.eventLog.textContent = `${nodes.eventLog.textContent}${line}\n`;
|
| 62 |
+
nodes.eventLog.scrollTop = nodes.eventLog.scrollHeight;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
function createAgent() {
|
| 66 |
+
const next = createPiAgent({
|
| 67 |
+
sandbox,
|
| 68 |
+
modelMode: () => nodes.mode.value,
|
| 69 |
+
device: () => nodes.device.value,
|
| 70 |
+
maxTokens: () => nodes.maxTokens.value,
|
| 71 |
+
temperature: () => nodes.temperature.value,
|
| 72 |
+
onModelStatus: setModelStatus,
|
| 73 |
+
});
|
| 74 |
+
next.subscribe((event) => {
|
| 75 |
+
switch (event.type) {
|
| 76 |
+
case "agent_start":
|
| 77 |
+
setStatus("Agent running");
|
| 78 |
+
logEvent("agent", "start");
|
| 79 |
+
break;
|
| 80 |
+
case "message_end":
|
| 81 |
+
renderTranscript();
|
| 82 |
+
break;
|
| 83 |
+
case "tool_execution_start":
|
| 84 |
+
logEvent("tool", `${event.toolName} started`);
|
| 85 |
+
break;
|
| 86 |
+
case "tool_execution_end":
|
| 87 |
+
logEvent("tool", `${event.toolName} finished`);
|
| 88 |
+
break;
|
| 89 |
+
case "agent_end":
|
| 90 |
+
setStatus("Ready");
|
| 91 |
+
renderTranscript();
|
| 92 |
+
refreshFiles().catch((error) => logEvent("files", error.message));
|
| 93 |
+
break;
|
| 94 |
+
default:
|
| 95 |
+
break;
|
| 96 |
+
}
|
| 97 |
+
});
|
| 98 |
+
return next;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
function resetAgent() {
|
| 102 |
+
agent.abort();
|
| 103 |
+
agent = createAgent();
|
| 104 |
+
renderTranscript();
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
function renderTranscript() {
|
| 108 |
+
const rendered = agent.state.messages
|
| 109 |
+
.map((message) => {
|
| 110 |
+
if (message.role === "toolResult") {
|
| 111 |
+
return `TOOL ${message.toolName}${message.isError ? " ERROR" : ""}\n${textFromContent(message.content)}`;
|
| 112 |
+
}
|
| 113 |
+
const heading = message.role.toUpperCase();
|
| 114 |
+
const toolCalls =
|
| 115 |
+
message.role === "assistant"
|
| 116 |
+
? message.content
|
| 117 |
+
.filter((part) => part.type === "toolCall")
|
| 118 |
+
.map((part) => `\nTOOL CALL ${part.name} ${JSON.stringify(part.arguments)}`)
|
| 119 |
+
.join("")
|
| 120 |
+
: "";
|
| 121 |
+
return `${heading}\n${textFromContent(message.content)}${toolCalls}`;
|
| 122 |
+
})
|
| 123 |
+
.join("\n\n");
|
| 124 |
+
nodes.transcript.textContent = rendered || "No messages yet.";
|
| 125 |
+
nodes.transcript.scrollTop = nodes.transcript.scrollHeight;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
async function refreshFiles() {
|
| 129 |
+
if (!sandbox.isReady) {
|
| 130 |
+
nodes.files.textContent = "Sandbox not booted.";
|
| 131 |
+
return;
|
| 132 |
+
}
|
| 133 |
+
nodes.files.textContent = await sandbox.listFiles(".");
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
async function bootSandbox() {
|
| 137 |
+
nodes.boot.disabled = true;
|
| 138 |
+
try {
|
| 139 |
+
await sandbox.boot();
|
| 140 |
+
await refreshFiles();
|
| 141 |
+
} catch (error) {
|
| 142 |
+
setSandboxStatus("Sandbox error");
|
| 143 |
+
logEvent("sandbox", error.stack || error.message || String(error));
|
| 144 |
+
} finally {
|
| 145 |
+
nodes.boot.disabled = false;
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
nodes.boot.addEventListener("click", bootSandbox);
|
| 150 |
+
|
| 151 |
+
nodes.reset.addEventListener("click", async () => {
|
| 152 |
+
nodes.reset.disabled = true;
|
| 153 |
+
try {
|
| 154 |
+
await sandbox.reset();
|
| 155 |
+
resetAgent();
|
| 156 |
+
await refreshFiles();
|
| 157 |
+
} catch (error) {
|
| 158 |
+
logEvent("reset", error.stack || error.message || String(error));
|
| 159 |
+
} finally {
|
| 160 |
+
nodes.reset.disabled = false;
|
| 161 |
+
}
|
| 162 |
+
});
|
| 163 |
+
|
| 164 |
+
nodes.demo.addEventListener("click", () => {
|
| 165 |
+
nodes.prompt.value =
|
| 166 |
+
"Create hello.js that prints the result of 21 * 2, run it with Node, and tell me the command output.";
|
| 167 |
+
});
|
| 168 |
+
|
| 169 |
+
nodes.run.addEventListener("click", async () => {
|
| 170 |
+
nodes.run.disabled = true;
|
| 171 |
+
try {
|
| 172 |
+
await bootSandbox();
|
| 173 |
+
await agent.prompt(nodes.prompt.value);
|
| 174 |
+
} catch (error) {
|
| 175 |
+
setStatus("Error");
|
| 176 |
+
logEvent("agent", error.stack || error.message || String(error));
|
| 177 |
+
} finally {
|
| 178 |
+
nodes.run.disabled = false;
|
| 179 |
+
}
|
| 180 |
+
});
|
| 181 |
+
|
| 182 |
+
nodes.mode.addEventListener("change", () => {
|
| 183 |
+
resetAgent();
|
| 184 |
+
setModelStatus(nodes.mode.value === "mock" ? "Deterministic test model" : "Model idle");
|
| 185 |
+
});
|
| 186 |
+
|
| 187 |
+
setStatus("Ready");
|
| 188 |
+
setSandboxStatus("Not booted");
|
| 189 |
+
setModelStatus(nodes.mode.value === "mock" ? "Deterministic test model" : "Model idle");
|
| 190 |
+
renderTranscript();
|
| 191 |
+
refreshFiles().catch(() => {});
|
| 192 |
+
|
src/piAgent.js
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Agent } from "@earendil-works/pi-agent-core";
|
| 2 |
+
import { createAssistantMessageEventStream, Type } from "@earendil-works/pi-ai";
|
| 3 |
+
import { env, pipeline } from "@huggingface/transformers";
|
| 4 |
+
|
| 5 |
+
const MODEL_ID = "Mike0021/MiniCPM5-1B-ONNX-Web";
|
| 6 |
+
|
| 7 |
+
const LOCAL_MODEL = {
|
| 8 |
+
id: MODEL_ID,
|
| 9 |
+
name: "MiniCPM5-1B ONNX Web",
|
| 10 |
+
api: "transformers-js",
|
| 11 |
+
provider: "huggingface-transformers-js",
|
| 12 |
+
baseUrl: "https://huggingface.co",
|
| 13 |
+
reasoning: false,
|
| 14 |
+
input: ["text"],
|
| 15 |
+
cost: {
|
| 16 |
+
input: 0,
|
| 17 |
+
output: 0,
|
| 18 |
+
cacheRead: 0,
|
| 19 |
+
cacheWrite: 0,
|
| 20 |
+
},
|
| 21 |
+
contextWindow: 4096,
|
| 22 |
+
maxTokens: 512,
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
const EMPTY_USAGE = {
|
| 26 |
+
input: 0,
|
| 27 |
+
output: 0,
|
| 28 |
+
cacheRead: 0,
|
| 29 |
+
cacheWrite: 0,
|
| 30 |
+
totalTokens: 0,
|
| 31 |
+
cost: {
|
| 32 |
+
input: 0,
|
| 33 |
+
output: 0,
|
| 34 |
+
cacheRead: 0,
|
| 35 |
+
cacheWrite: 0,
|
| 36 |
+
total: 0,
|
| 37 |
+
},
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
function textFromContent(content) {
|
| 41 |
+
if (typeof content === "string") return content;
|
| 42 |
+
if (!Array.isArray(content)) return "";
|
| 43 |
+
return content
|
| 44 |
+
.filter((part) => part.type === "text")
|
| 45 |
+
.map((part) => part.text)
|
| 46 |
+
.join("\n");
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
function now() {
|
| 50 |
+
return Date.now();
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
function createMessage(content, stopReason = "stop") {
|
| 54 |
+
return {
|
| 55 |
+
role: "assistant",
|
| 56 |
+
content,
|
| 57 |
+
api: LOCAL_MODEL.api,
|
| 58 |
+
provider: LOCAL_MODEL.provider,
|
| 59 |
+
model: LOCAL_MODEL.id,
|
| 60 |
+
usage: EMPTY_USAGE,
|
| 61 |
+
stopReason,
|
| 62 |
+
timestamp: now(),
|
| 63 |
+
};
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
function stringifyToolResult(message) {
|
| 67 |
+
const text = textFromContent(message.content);
|
| 68 |
+
return `${message.toolName}(${message.toolCallId}) ${message.isError ? "failed" : "succeeded"}:\n${text}`;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
function buildPrompt(context) {
|
| 72 |
+
const tools = (context.tools || []).map((tool) => ({
|
| 73 |
+
name: tool.name,
|
| 74 |
+
description: tool.description,
|
| 75 |
+
parameters: tool.parameters,
|
| 76 |
+
}));
|
| 77 |
+
const transcript = context.messages
|
| 78 |
+
.slice(-8)
|
| 79 |
+
.map((message) => {
|
| 80 |
+
if (message.role === "toolResult") return `TOOL_RESULT:\n${stringifyToolResult(message)}`;
|
| 81 |
+
return `${message.role.toUpperCase()}:\n${textFromContent(message.content)}`;
|
| 82 |
+
})
|
| 83 |
+
.join("\n\n");
|
| 84 |
+
|
| 85 |
+
return `${context.systemPrompt || ""}
|
| 86 |
+
|
| 87 |
+
You are running as a pi Agent inside a browser-only app. The sandbox is a WebContainer: it has a virtual filesystem and can spawn browser-contained Node.js processes.
|
| 88 |
+
|
| 89 |
+
Use tools by returning strict JSON only. Do not use markdown.
|
| 90 |
+
|
| 91 |
+
To call tools:
|
| 92 |
+
{"toolCalls":[{"tool":"write_file","args":{"path":"hello.js","content":"console.log(2 + 2)\\n"}},{"tool":"run_command","args":{"command":"node","args":["hello.js"]}}]}
|
| 93 |
+
|
| 94 |
+
To answer the user after tool results:
|
| 95 |
+
{"final":"Short answer that explains what happened."}
|
| 96 |
+
|
| 97 |
+
Available tools:
|
| 98 |
+
${JSON.stringify(tools, null, 2)}
|
| 99 |
+
|
| 100 |
+
Conversation:
|
| 101 |
+
${transcript}
|
| 102 |
+
|
| 103 |
+
Return JSON now.`;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
function extractJsonPayload(text) {
|
| 107 |
+
const trimmed = String(text || "").trim();
|
| 108 |
+
const fence = trimmed.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
| 109 |
+
const candidate = fence ? fence[1].trim() : trimmed;
|
| 110 |
+
const firstBrace = candidate.indexOf("{");
|
| 111 |
+
const firstBracket = candidate.indexOf("[");
|
| 112 |
+
const starts = [firstBrace, firstBracket].filter((index) => index >= 0);
|
| 113 |
+
if (starts.length === 0) return null;
|
| 114 |
+
const start = Math.min(...starts);
|
| 115 |
+
const lastBrace = candidate.lastIndexOf("}");
|
| 116 |
+
const lastBracket = candidate.lastIndexOf("]");
|
| 117 |
+
const end = Math.max(lastBrace, lastBracket);
|
| 118 |
+
if (end <= start) return null;
|
| 119 |
+
try {
|
| 120 |
+
return JSON.parse(candidate.slice(start, end + 1));
|
| 121 |
+
} catch {
|
| 122 |
+
return null;
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
function normalizeToolCalls(payload) {
|
| 127 |
+
if (!payload) return [];
|
| 128 |
+
const rawCalls = Array.isArray(payload) ? payload : payload.toolCalls || payload.tools || payload.actions || [];
|
| 129 |
+
if (!Array.isArray(rawCalls)) return [];
|
| 130 |
+
return rawCalls
|
| 131 |
+
.map((call, index) => ({
|
| 132 |
+
type: "toolCall",
|
| 133 |
+
id: `tool-${now()}-${index}`,
|
| 134 |
+
name: String(call.tool || call.name || ""),
|
| 135 |
+
arguments: call.args || call.arguments || {},
|
| 136 |
+
}))
|
| 137 |
+
.filter((call) => call.name);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
function normalizeFinalText(payload, fallback) {
|
| 141 |
+
if (payload && typeof payload.final === "string") return payload.final;
|
| 142 |
+
if (payload && typeof payload.message === "string") return payload.message;
|
| 143 |
+
if (payload && typeof payload.answer === "string") return payload.answer;
|
| 144 |
+
return String(fallback || "").trim() || "Done.";
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
function mockPlan(context) {
|
| 148 |
+
const last = context.messages[context.messages.length - 1];
|
| 149 |
+
if (last?.role === "toolResult") {
|
| 150 |
+
const results = context.messages.filter((message) => message.role === "toolResult").slice(-4).map(stringifyToolResult);
|
| 151 |
+
return {
|
| 152 |
+
final: `The sandbox work completed.\n\n${results.join("\n\n")}`,
|
| 153 |
+
};
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
const userText = textFromContent(last?.content || "").toLowerCase();
|
| 157 |
+
if (userText.includes("read")) {
|
| 158 |
+
return {
|
| 159 |
+
toolCalls: [{ tool: "read_file", args: { path: "hello.js" } }],
|
| 160 |
+
};
|
| 161 |
+
}
|
| 162 |
+
if (userText.includes("list")) {
|
| 163 |
+
return {
|
| 164 |
+
toolCalls: [{ tool: "list_files", args: { path: "." } }],
|
| 165 |
+
};
|
| 166 |
+
}
|
| 167 |
+
return {
|
| 168 |
+
toolCalls: [
|
| 169 |
+
{
|
| 170 |
+
tool: "write_file",
|
| 171 |
+
args: {
|
| 172 |
+
path: "hello.js",
|
| 173 |
+
content: 'const value = 21 * 2;\nconsole.log(`pi sandbox result: ${value}`);\n',
|
| 174 |
+
},
|
| 175 |
+
},
|
| 176 |
+
{
|
| 177 |
+
tool: "run_command",
|
| 178 |
+
args: {
|
| 179 |
+
command: "node",
|
| 180 |
+
args: ["hello.js"],
|
| 181 |
+
},
|
| 182 |
+
},
|
| 183 |
+
],
|
| 184 |
+
};
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
function emitFinal(stream, message, text = "") {
|
| 188 |
+
stream.push({ type: "start", partial: { ...message, content: [{ type: "text", text: "" }] } });
|
| 189 |
+
if (text) {
|
| 190 |
+
const partial = { ...message, content: [{ type: "text", text }] };
|
| 191 |
+
stream.push({ type: "text_start", contentIndex: 0, partial: { ...message, content: [{ type: "text", text: "" }] } });
|
| 192 |
+
stream.push({ type: "text_delta", contentIndex: 0, delta: text, partial });
|
| 193 |
+
stream.push({ type: "text_end", contentIndex: 0, content: text, partial });
|
| 194 |
+
}
|
| 195 |
+
if (message.stopReason === "error" || message.stopReason === "aborted") {
|
| 196 |
+
stream.push({ type: "error", reason: message.stopReason, error: message });
|
| 197 |
+
} else {
|
| 198 |
+
stream.push({ type: "done", reason: message.stopReason, message });
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
export function createPiAgent({ sandbox, modelMode, device, maxTokens, temperature, onModelStatus = () => {} }) {
|
| 203 |
+
env.allowLocalModels = false;
|
| 204 |
+
env.allowRemoteModels = true;
|
| 205 |
+
env.backends.onnx.wasm.numThreads = Math.min(4, navigator.hardwareConcurrency || 4);
|
| 206 |
+
|
| 207 |
+
let generatorPromise = null;
|
| 208 |
+
let generatorKey = "";
|
| 209 |
+
|
| 210 |
+
async function getGenerator() {
|
| 211 |
+
const key = `${MODEL_ID}:${device()}`;
|
| 212 |
+
if (!generatorPromise || generatorKey !== key) {
|
| 213 |
+
generatorKey = key;
|
| 214 |
+
onModelStatus(`Loading ${device()}`);
|
| 215 |
+
generatorPromise = pipeline("text-generation", MODEL_ID, {
|
| 216 |
+
dtype: "q4",
|
| 217 |
+
device: device(),
|
| 218 |
+
progress_callback: (event) => {
|
| 219 |
+
if (event.status === "progress") {
|
| 220 |
+
onModelStatus(`${event.file} ${Math.round(event.progress)}%`);
|
| 221 |
+
} else if (event.status) {
|
| 222 |
+
onModelStatus(event.status);
|
| 223 |
+
}
|
| 224 |
+
},
|
| 225 |
+
});
|
| 226 |
+
}
|
| 227 |
+
return generatorPromise;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
async function producePlan(context, signal) {
|
| 231 |
+
if (modelMode() === "mock") {
|
| 232 |
+
return JSON.stringify(mockPlan(context));
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
const generator = await getGenerator();
|
| 236 |
+
if (signal?.aborted) throw new Error("Aborted");
|
| 237 |
+
const result = await generator(buildPrompt(context), {
|
| 238 |
+
max_new_tokens: Number(maxTokens()) || 128,
|
| 239 |
+
temperature: Number(temperature()) || 0,
|
| 240 |
+
do_sample: Number(temperature()) > 0,
|
| 241 |
+
return_full_text: false,
|
| 242 |
+
});
|
| 243 |
+
onModelStatus("Model ready");
|
| 244 |
+
return result?.[0]?.generated_text ?? "";
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
function streamFn(_model, context, options = {}) {
|
| 248 |
+
const stream = createAssistantMessageEventStream();
|
| 249 |
+
queueMicrotask(async () => {
|
| 250 |
+
try {
|
| 251 |
+
const generated = await producePlan(context, options.signal);
|
| 252 |
+
const payload = extractJsonPayload(generated);
|
| 253 |
+
const lastMessage = context.messages[context.messages.length - 1];
|
| 254 |
+
const forceFinal = lastMessage?.role === "toolResult";
|
| 255 |
+
const fallbackPayload = payload ? null : mockPlan(context);
|
| 256 |
+
const toolCalls = forceFinal ? [] : normalizeToolCalls(payload || fallbackPayload);
|
| 257 |
+
if (toolCalls.length > 0) {
|
| 258 |
+
const message = createMessage([{ type: "text", text: "Using sandbox tools." }, ...toolCalls], "toolUse");
|
| 259 |
+
emitFinal(stream, message, "Using sandbox tools.");
|
| 260 |
+
return;
|
| 261 |
+
}
|
| 262 |
+
const text = normalizeFinalText(payload || fallbackPayload, generated);
|
| 263 |
+
const message = createMessage([{ type: "text", text }], "stop");
|
| 264 |
+
emitFinal(stream, message, text);
|
| 265 |
+
} catch (error) {
|
| 266 |
+
const text = error instanceof Error ? error.message : String(error);
|
| 267 |
+
const message = {
|
| 268 |
+
...createMessage([{ type: "text", text }], options.signal?.aborted ? "aborted" : "error"),
|
| 269 |
+
errorMessage: text,
|
| 270 |
+
};
|
| 271 |
+
emitFinal(stream, message, text);
|
| 272 |
+
}
|
| 273 |
+
});
|
| 274 |
+
return stream;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
const tools = [
|
| 278 |
+
{
|
| 279 |
+
name: "list_files",
|
| 280 |
+
label: "List files",
|
| 281 |
+
description: "List files in the sandbox workspace.",
|
| 282 |
+
parameters: Type.Object({
|
| 283 |
+
path: Type.Optional(Type.String()),
|
| 284 |
+
}),
|
| 285 |
+
execute: async (_id, args) => {
|
| 286 |
+
const output = await sandbox.listFiles(args.path || ".");
|
| 287 |
+
return { content: [{ type: "text", text: output }], details: { output } };
|
| 288 |
+
},
|
| 289 |
+
},
|
| 290 |
+
{
|
| 291 |
+
name: "read_file",
|
| 292 |
+
label: "Read file",
|
| 293 |
+
description: "Read a UTF-8 file from the sandbox workspace.",
|
| 294 |
+
parameters: Type.Object({
|
| 295 |
+
path: Type.String(),
|
| 296 |
+
}),
|
| 297 |
+
execute: async (_id, args) => {
|
| 298 |
+
const output = await sandbox.readFile(args.path);
|
| 299 |
+
return { content: [{ type: "text", text: output }], details: { path: args.path, output } };
|
| 300 |
+
},
|
| 301 |
+
},
|
| 302 |
+
{
|
| 303 |
+
name: "write_file",
|
| 304 |
+
label: "Write file",
|
| 305 |
+
description: "Create or replace a UTF-8 file inside the sandbox workspace.",
|
| 306 |
+
parameters: Type.Object({
|
| 307 |
+
path: Type.String(),
|
| 308 |
+
content: Type.String(),
|
| 309 |
+
}),
|
| 310 |
+
execute: async (_id, args) => {
|
| 311 |
+
const output = await sandbox.writeFile(args.path, args.content);
|
| 312 |
+
return { content: [{ type: "text", text: output }], details: { path: args.path } };
|
| 313 |
+
},
|
| 314 |
+
},
|
| 315 |
+
{
|
| 316 |
+
name: "run_command",
|
| 317 |
+
label: "Run command",
|
| 318 |
+
description: "Spawn a process inside the browser-only WebContainer sandbox.",
|
| 319 |
+
parameters: Type.Object({
|
| 320 |
+
command: Type.String(),
|
| 321 |
+
args: Type.Optional(Type.Array(Type.String())),
|
| 322 |
+
timeoutMs: Type.Optional(Type.Number()),
|
| 323 |
+
}),
|
| 324 |
+
execute: async (_id, args) => {
|
| 325 |
+
const result = await sandbox.runCommand(args.command, args.args || [], args.timeoutMs || 10000);
|
| 326 |
+
const text = `$ ${result.command}\nexit ${result.exitCode}\n${result.output}`;
|
| 327 |
+
return {
|
| 328 |
+
content: [{ type: "text", text }],
|
| 329 |
+
details: result,
|
| 330 |
+
};
|
| 331 |
+
},
|
| 332 |
+
executionMode: "sequential",
|
| 333 |
+
},
|
| 334 |
+
];
|
| 335 |
+
|
| 336 |
+
return new Agent({
|
| 337 |
+
initialState: {
|
| 338 |
+
model: LOCAL_MODEL,
|
| 339 |
+
systemPrompt:
|
| 340 |
+
"You are Pi Web Agent. Use the sandbox tools for filesystem or command tasks, then give concise results.",
|
| 341 |
+
tools,
|
| 342 |
+
},
|
| 343 |
+
streamFn,
|
| 344 |
+
toolExecution: "sequential",
|
| 345 |
+
});
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
export { MODEL_ID };
|
src/sandbox.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const BOOT_FILES = {
|
| 2 |
+
"package.json": {
|
| 3 |
+
file: {
|
| 4 |
+
contents: JSON.stringify(
|
| 5 |
+
{
|
| 6 |
+
type: "module",
|
| 7 |
+
scripts: {
|
| 8 |
+
demo: "node hello.js",
|
| 9 |
+
},
|
| 10 |
+
dependencies: {},
|
| 11 |
+
devDependencies: {},
|
| 12 |
+
},
|
| 13 |
+
null,
|
| 14 |
+
2,
|
| 15 |
+
),
|
| 16 |
+
},
|
| 17 |
+
},
|
| 18 |
+
"README.md": {
|
| 19 |
+
file: {
|
| 20 |
+
contents:
|
| 21 |
+
"# Pi web sandbox\n\nThis filesystem and every spawned process run inside WebContainers in this browser tab.\n",
|
| 22 |
+
},
|
| 23 |
+
},
|
| 24 |
+
"hello.js": {
|
| 25 |
+
file: {
|
| 26 |
+
contents: 'console.log("hello from the browser sandbox");\nconsole.log(2 + 2);\n',
|
| 27 |
+
},
|
| 28 |
+
},
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
const MAX_OUTPUT_CHARS = 16000;
|
| 32 |
+
const DEFAULT_TIMEOUT_MS = 10000;
|
| 33 |
+
|
| 34 |
+
export function createSandbox({ onLog = () => {}, onStatus = () => {} } = {}) {
|
| 35 |
+
let instance = null;
|
| 36 |
+
let booting = null;
|
| 37 |
+
let root = "";
|
| 38 |
+
|
| 39 |
+
async function boot() {
|
| 40 |
+
if (instance) return instance;
|
| 41 |
+
if (booting) return booting;
|
| 42 |
+
|
| 43 |
+
booting = (async () => {
|
| 44 |
+
if (!globalThis.crossOriginIsolated) {
|
| 45 |
+
throw new Error("WebContainers need cross-origin isolation. Start this app through Vite or another server that sends COOP/COEP headers.");
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
onStatus("Booting WebContainer");
|
| 49 |
+
const { WebContainer } = await import("@webcontainer/api");
|
| 50 |
+
instance = await WebContainer.boot({
|
| 51 |
+
coep: "credentialless",
|
| 52 |
+
workdirName: "workspace",
|
| 53 |
+
});
|
| 54 |
+
root = instance.workdir;
|
| 55 |
+
await instance.mount(BOOT_FILES);
|
| 56 |
+
onLog(`sandbox booted at ${root}`);
|
| 57 |
+
onStatus("Sandbox ready");
|
| 58 |
+
return instance;
|
| 59 |
+
})();
|
| 60 |
+
|
| 61 |
+
try {
|
| 62 |
+
return await booting;
|
| 63 |
+
} finally {
|
| 64 |
+
booting = null;
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
function assertRelativePath(path) {
|
| 69 |
+
const value = String(path || "").trim().replace(/^\/+/, "");
|
| 70 |
+
if (!value || value.includes("\0")) {
|
| 71 |
+
throw new Error("Path is required.");
|
| 72 |
+
}
|
| 73 |
+
const segments = value.split("/").filter(Boolean);
|
| 74 |
+
if (segments.some((segment) => segment === "." || segment === "..")) {
|
| 75 |
+
throw new Error("Paths must stay inside the sandbox workspace.");
|
| 76 |
+
}
|
| 77 |
+
return segments.join("/");
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
function toWorkspacePath(path) {
|
| 81 |
+
return assertRelativePath(path);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
async function reset() {
|
| 85 |
+
const wc = await boot();
|
| 86 |
+
const entries = await wc.fs.readdir(".");
|
| 87 |
+
await Promise.all(
|
| 88 |
+
entries.map((entry) =>
|
| 89 |
+
wc.fs.rm(entry, {
|
| 90 |
+
force: true,
|
| 91 |
+
recursive: true,
|
| 92 |
+
}),
|
| 93 |
+
),
|
| 94 |
+
);
|
| 95 |
+
await wc.mount(BOOT_FILES);
|
| 96 |
+
onLog("sandbox reset");
|
| 97 |
+
return "Sandbox reset to the starter project.";
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
async function listFiles(path = ".") {
|
| 101 |
+
const wc = await boot();
|
| 102 |
+
const target = path === "." || path === "" ? "." : toWorkspacePath(path);
|
| 103 |
+
const entries = await wc.fs.readdir(target, { withFileTypes: true });
|
| 104 |
+
return entries.map((entry) => `${entry.isDirectory() ? "dir " : "file"} ${entry.name}`).join("\n") || "(empty)";
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
async function readFile(path) {
|
| 108 |
+
const wc = await boot();
|
| 109 |
+
return await wc.fs.readFile(toWorkspacePath(path), "utf-8");
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
async function writeFile(path, content) {
|
| 113 |
+
const wc = await boot();
|
| 114 |
+
const relative = assertRelativePath(path);
|
| 115 |
+
const parent = relative.split("/").slice(0, -1).join("/");
|
| 116 |
+
if (parent) {
|
| 117 |
+
await wc.fs.mkdir(parent, { recursive: true });
|
| 118 |
+
}
|
| 119 |
+
await wc.fs.writeFile(relative, String(content ?? ""));
|
| 120 |
+
onLog(`wrote ${relative}`);
|
| 121 |
+
return `Wrote ${relative}`;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
async function runCommand(command, args = [], timeoutMs = DEFAULT_TIMEOUT_MS) {
|
| 125 |
+
const wc = await boot();
|
| 126 |
+
const cmd = String(command || "").trim();
|
| 127 |
+
if (!cmd) throw new Error("Command is required.");
|
| 128 |
+
const cleanArgs = Array.isArray(args) ? args.map((arg) => String(arg)) : [];
|
| 129 |
+
|
| 130 |
+
onLog(`$ ${[cmd, ...cleanArgs].join(" ")}`);
|
| 131 |
+
const process = await wc.spawn(cmd, cleanArgs, {
|
| 132 |
+
terminal: {
|
| 133 |
+
cols: 96,
|
| 134 |
+
rows: 28,
|
| 135 |
+
},
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
let output = "";
|
| 139 |
+
const reader = process.output.getReader();
|
| 140 |
+
const pump = (async () => {
|
| 141 |
+
while (true) {
|
| 142 |
+
const { done, value } = await reader.read();
|
| 143 |
+
if (done) break;
|
| 144 |
+
output += value;
|
| 145 |
+
if (output.length > MAX_OUTPUT_CHARS) {
|
| 146 |
+
output = `${output.slice(0, MAX_OUTPUT_CHARS)}\n[output truncated]`;
|
| 147 |
+
process.kill();
|
| 148 |
+
break;
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
})();
|
| 152 |
+
|
| 153 |
+
const timeout = new Promise((resolve) => {
|
| 154 |
+
setTimeout(() => {
|
| 155 |
+
process.kill();
|
| 156 |
+
resolve("timeout");
|
| 157 |
+
}, Number(timeoutMs) || DEFAULT_TIMEOUT_MS);
|
| 158 |
+
});
|
| 159 |
+
|
| 160 |
+
const exitCode = await Promise.race([process.exit, timeout]);
|
| 161 |
+
await pump.catch(() => {});
|
| 162 |
+
const normalizedExitCode = exitCode === "timeout" ? 124 : exitCode;
|
| 163 |
+
const result = {
|
| 164 |
+
command: [cmd, ...cleanArgs].join(" "),
|
| 165 |
+
exitCode: normalizedExitCode,
|
| 166 |
+
output: output.trimEnd(),
|
| 167 |
+
};
|
| 168 |
+
onLog(`exit ${normalizedExitCode}`);
|
| 169 |
+
return result;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
return {
|
| 173 |
+
boot,
|
| 174 |
+
reset,
|
| 175 |
+
listFiles,
|
| 176 |
+
readFile,
|
| 177 |
+
writeFile,
|
| 178 |
+
runCommand,
|
| 179 |
+
get isReady() {
|
| 180 |
+
return Boolean(instance);
|
| 181 |
+
},
|
| 182 |
+
};
|
| 183 |
+
}
|
src/styles.css
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
:root {
|
| 2 |
+
color: #172033;
|
| 3 |
+
background: #f3f6f9;
|
| 4 |
+
font-family:
|
| 5 |
+
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
* {
|
| 9 |
+
box-sizing: border-box;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
body {
|
| 13 |
+
margin: 0;
|
| 14 |
+
min-width: 320px;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
button,
|
| 18 |
+
input,
|
| 19 |
+
select,
|
| 20 |
+
textarea {
|
| 21 |
+
font: inherit;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
#app {
|
| 25 |
+
min-height: 100vh;
|
| 26 |
+
padding: 18px;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.shell {
|
| 30 |
+
width: min(1280px, 100%);
|
| 31 |
+
margin: 0 auto;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
.topbar {
|
| 35 |
+
display: flex;
|
| 36 |
+
align-items: end;
|
| 37 |
+
justify-content: space-between;
|
| 38 |
+
gap: 16px;
|
| 39 |
+
padding: 8px 0 18px;
|
| 40 |
+
border-bottom: 1px solid #c7d2df;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
h1,
|
| 44 |
+
h2,
|
| 45 |
+
p {
|
| 46 |
+
margin: 0;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
h1 {
|
| 50 |
+
font-size: 38px;
|
| 51 |
+
line-height: 1;
|
| 52 |
+
letter-spacing: 0;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
h2 {
|
| 56 |
+
font-size: 15px;
|
| 57 |
+
line-height: 1.2;
|
| 58 |
+
color: #405064;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
#model-label {
|
| 62 |
+
margin-top: 8px;
|
| 63 |
+
color: #5d6c7f;
|
| 64 |
+
overflow-wrap: anywhere;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
.status-stack {
|
| 68 |
+
display: flex;
|
| 69 |
+
flex-wrap: wrap;
|
| 70 |
+
justify-content: end;
|
| 71 |
+
gap: 8px;
|
| 72 |
+
max-width: 620px;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
.status {
|
| 76 |
+
min-height: 34px;
|
| 77 |
+
max-width: 260px;
|
| 78 |
+
padding: 7px 10px;
|
| 79 |
+
border: 1px solid #b7c4d3;
|
| 80 |
+
border-radius: 6px;
|
| 81 |
+
background: #ffffff;
|
| 82 |
+
color: #314155;
|
| 83 |
+
overflow-wrap: anywhere;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
.controls {
|
| 87 |
+
display: grid;
|
| 88 |
+
grid-template-columns: 1.2fr 1fr 1fr 1fr;
|
| 89 |
+
gap: 12px;
|
| 90 |
+
margin: 18px 0 12px;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
label {
|
| 94 |
+
display: grid;
|
| 95 |
+
gap: 6px;
|
| 96 |
+
color: #526173;
|
| 97 |
+
font-size: 14px;
|
| 98 |
+
font-weight: 650;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
select,
|
| 102 |
+
input,
|
| 103 |
+
textarea {
|
| 104 |
+
width: 100%;
|
| 105 |
+
border: 1px solid #b8c5d4;
|
| 106 |
+
border-radius: 6px;
|
| 107 |
+
background: #ffffff;
|
| 108 |
+
color: #152033;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
select,
|
| 112 |
+
input {
|
| 113 |
+
height: 42px;
|
| 114 |
+
padding: 0 10px;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
.command-row {
|
| 118 |
+
display: flex;
|
| 119 |
+
flex-wrap: wrap;
|
| 120 |
+
gap: 10px;
|
| 121 |
+
margin-bottom: 14px;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
button {
|
| 125 |
+
min-width: 116px;
|
| 126 |
+
height: 42px;
|
| 127 |
+
padding: 0 16px;
|
| 128 |
+
border: 0;
|
| 129 |
+
border-radius: 6px;
|
| 130 |
+
background: #176b6c;
|
| 131 |
+
color: #ffffff;
|
| 132 |
+
font-weight: 750;
|
| 133 |
+
cursor: pointer;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
button:nth-child(2) {
|
| 137 |
+
background: #4f5f73;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
button:nth-child(3) {
|
| 141 |
+
background: #7a4d18;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
button:disabled {
|
| 145 |
+
cursor: wait;
|
| 146 |
+
opacity: 0.64;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
.workspace {
|
| 150 |
+
display: grid;
|
| 151 |
+
gap: 14px;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
.prompt-pane {
|
| 155 |
+
display: grid;
|
| 156 |
+
gap: 10px;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
textarea {
|
| 160 |
+
min-height: 116px;
|
| 161 |
+
resize: vertical;
|
| 162 |
+
padding: 13px;
|
| 163 |
+
line-height: 1.45;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
#run {
|
| 167 |
+
width: fit-content;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
.panes {
|
| 171 |
+
display: grid;
|
| 172 |
+
grid-template-columns: 1.35fr 0.9fr;
|
| 173 |
+
gap: 12px;
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
.pane {
|
| 177 |
+
display: grid;
|
| 178 |
+
gap: 8px;
|
| 179 |
+
min-width: 0;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
.pane.wide {
|
| 183 |
+
grid-column: 1 / -1;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
pre {
|
| 187 |
+
min-height: 220px;
|
| 188 |
+
max-height: 360px;
|
| 189 |
+
margin: 0;
|
| 190 |
+
padding: 14px;
|
| 191 |
+
border: 1px solid #b8c5d4;
|
| 192 |
+
border-radius: 6px;
|
| 193 |
+
background: #fbfcfd;
|
| 194 |
+
color: #162033;
|
| 195 |
+
line-height: 1.45;
|
| 196 |
+
white-space: pre-wrap;
|
| 197 |
+
overflow: auto;
|
| 198 |
+
overflow-wrap: anywhere;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
#event-log {
|
| 202 |
+
min-height: 150px;
|
| 203 |
+
max-height: 220px;
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
@media (max-width: 780px) {
|
| 207 |
+
#app {
|
| 208 |
+
padding: 12px;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
.topbar {
|
| 212 |
+
align-items: stretch;
|
| 213 |
+
flex-direction: column;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
.status-stack {
|
| 217 |
+
justify-content: start;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
.controls,
|
| 221 |
+
.panes {
|
| 222 |
+
grid-template-columns: 1fr;
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
|
vite.config.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { defineConfig } from "vite";
|
| 2 |
+
|
| 3 |
+
const headers = {
|
| 4 |
+
"Cross-Origin-Embedder-Policy": "credentialless",
|
| 5 |
+
"Cross-Origin-Opener-Policy": "same-origin",
|
| 6 |
+
};
|
| 7 |
+
|
| 8 |
+
export default defineConfig({
|
| 9 |
+
server: {
|
| 10 |
+
headers,
|
| 11 |
+
},
|
| 12 |
+
preview: {
|
| 13 |
+
headers,
|
| 14 |
+
},
|
| 15 |
+
});
|
| 16 |
+
|