Mike0021 commited on
Commit
4118aba
·
verified ·
1 Parent(s): 8316a28

Deploy Pi CLI web terminal

Browse files
README.md CHANGED
@@ -1,10 +1,155 @@
1
  ---
2
- title: Pi CLI Web Static
3
- emoji: 📊
4
- colorFrom: pink
5
- colorTo: red
6
  sdk: static
7
- pinned: false
 
 
 
 
 
 
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Pi CLI Web
 
 
 
3
  sdk: static
4
+ app_file: index.html
5
+ fullWidth: true
6
+ models:
7
+ - onnx-community/Qwen2.5-Coder-0.5B-Instruct
8
+ - onnx-community/Qwen3-0.6B-ONNX
9
+ - Mike0021/MiniCPM5-1B-ONNX-Web
10
+ custom_headers:
11
+ cross-origin-embedder-policy: credentialless
12
+ cross-origin-opener-policy: same-origin
13
+ cross-origin-resource-policy: cross-origin
14
  ---
15
 
16
+ # Pi CLI Web
17
+
18
+ This workspace ships a browser-only port of the `pi` CLI backed by Transformers.js, WebContainers, and a real terminal surface. The UI uses `ghostty-web` first, with `@xterm/xterm` as a fallback, and exposes Pi's built-in tool names: `read`, `bash`, `edit`, `write`, plus read-only `grep`, `find`, and `ls`.
19
+
20
+ The default planner is `onnx-community/Qwen2.5-Coder-0.5B-Instruct` because it produced the strongest browser result in the local task suite; Qwen3 0.6B and the converted MiniCPM5 model remain selectable for comparison.
21
+
22
+ Published artifact: https://huggingface.co/Mike0021/MiniCPM5-1B-ONNX-Web
23
+
24
+ The required runtime layout is:
25
+
26
+ - `config.json`, `generation_config.json`, tokenizer files, and `chat_template.jinja` at the repo root
27
+ - q4 ONNX weights at `onnx/model_q4.onnx`
28
+ - `config.json` includes `transformers.js_config.dtype = "q4"` so the default loader selects the web-sized artifact
29
+
30
+ 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.
31
+
32
+ ## Run the Web App
33
+
34
+ ```bash
35
+ npm install
36
+ npm run dev
37
+ ```
38
+
39
+ Open http://localhost:5173/.
40
+
41
+ The app uses:
42
+
43
+ - `@earendil-works/pi-agent-core` for the agent loop, transcript state, and tool execution.
44
+ - `@earendil-works/pi-coding-agent` as the installed CLI contract for parity checks against `pi --help` and `pi --version`.
45
+ - `ghostty-web` as the terminal frontend, with `@xterm/xterm` fallback.
46
+ - `@huggingface/transformers` with `onnx-community/Qwen2.5-Coder-0.5B-Instruct` as the default local browser planner.
47
+ - `@webcontainer/api` for the client-only sandbox with a virtual filesystem and browser-contained Node.js processes.
48
+
49
+ 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 an ONNX model. The local model defaults to a tested 256-token generation budget in WASM mode and supports budgets up to 8192 through the `tokens=` query parameter and `/settings tokens=<n>`.
50
+
51
+ The Static Space uses the same isolation policy through `custom_headers` in this README frontmatter. The app is built with `npm run build`; the generated `dist/index.html` and `dist/assets/` are uploaded to the Space root as `index.html` and `assets/`.
52
+
53
+ ## Test the CLI Web App
54
+
55
+ Start the dev server, then run:
56
+
57
+ ```bash
58
+ npm run smoke:web
59
+ ```
60
+
61
+ The smoke test opens Chromium, confirms `crossOriginIsolated`, verifies the terminal startup, runs `/help`, executes a direct `!!node ...` command, then submits a deterministic Pi task that writes `hello.js`, runs `bash`/Node in WebContainer, and checks for `pi sandbox result: 42`.
62
+
63
+ To compare the web terminal contract against the installed real CLI:
64
+
65
+ ```bash
66
+ npm run parity:cli
67
+ ```
68
+
69
+ This checks `pi --version`, the `pi --help` contract, slash commands, and the built-in tool names exposed by the browser terminal.
70
+
71
+ For the heavier end-to-end check with the real ONNX model in browser WASM mode:
72
+
73
+ ```bash
74
+ npm run smoke:local-model
75
+ ```
76
+
77
+ This downloads/loads the q4 ONNX artifact in Chrome, runs the same pi/WebContainer task, and checks that the model reaches `Model ready` before the sandbox result is accepted.
78
+
79
+ The complex smoke test covers simple code execution, installing and using an npm package, and a multi-file ES module task:
80
+
81
+ ```bash
82
+ npm run smoke:complex
83
+ ```
84
+
85
+ The sandbox can install and use Node packages through the same Pi `bash` tool, for example `npm install is-number@7.0.0` followed by `node check-package.mjs`.
86
+
87
+ To probe larger browser generation budgets:
88
+
89
+ ```bash
90
+ TOKEN_BUDGETS=80,256,2048,8192 npm run probe:tokens
91
+ ```
92
+
93
+ Measured local WASM results with Qwen2.5-Coder 0.5B:
94
+
95
+ - `npm run smoke:web` passed in deterministic mode using the `ghostty-web` terminal.
96
+ - `npm run parity:cli` passed against `@earendil-works/pi-coding-agent@0.77.0`.
97
+ - `MAX_NEW_TOKENS=80 npm run smoke:local-model` passed with the real browser model.
98
+ - `MAX_NEW_TOKENS=256 npm run smoke:complex` passed simple, npm dependency, and multi-file module tasks with the real browser model.
99
+ - `TOKEN_BUDGETS=80,160,256,512,1024,2048,4096,8192 npm run probe:tokens` passed. Higher caps were accepted; for the probe task the model stopped naturally before using the full cap.
100
+
101
+ ## Verify the Published Artifact
102
+
103
+ ```bash
104
+ npm install
105
+ node scripts/verify_tjs_model.mjs Mike0021/MiniCPM5-1B-ONNX-Web
106
+ ```
107
+
108
+ 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.
109
+
110
+ ## Convert and Upload
111
+
112
+ The published repo was produced locally with a CPU fp16 export followed by q4 ONNX quantization:
113
+
114
+ ```bash
115
+ uv run --python 3.12 \
116
+ --with "numpy<2" \
117
+ --with "transformers==4.57.6" \
118
+ --with "optimum[onnx]" \
119
+ --with "onnxruntime==1.20.1" \
120
+ --with onnxslim \
121
+ --with "huggingface_hub>=0.33" \
122
+ --with accelerate \
123
+ --with sentencepiece \
124
+ --with protobuf \
125
+ scripts/convert_minicpm5_tjs.py \
126
+ --source-model openbmb/MiniCPM5-1B \
127
+ --target-repo Mike0021/MiniCPM5-1B-ONNX-Web \
128
+ --output-dir output/MiniCPM5-1B-ONNX-Web \
129
+ --work-dir output/minicpm5-work \
130
+ --device cpu \
131
+ --export-dtype fp16
132
+ ```
133
+
134
+ For a clean remote conversion, the same script can be run on Hugging Face Jobs with a configured Hub token:
135
+
136
+ ```bash
137
+ hf repos create Mike0021/MiniCPM5-1B-ONNX-Web --repo-type model --exist-ok
138
+ hf jobs uv run scripts/convert_minicpm5_tjs.py \
139
+ --flavor l4x1 \
140
+ --timeout 6h \
141
+ --secrets HF_TOKEN \
142
+ --with "numpy<2" \
143
+ --with "transformers==4.57.6" \
144
+ --with "optimum[onnx]" \
145
+ --with "onnxruntime==1.20.1" \
146
+ --with onnxslim \
147
+ --with "huggingface_hub>=0.33" \
148
+ --with accelerate \
149
+ --with sentencepiece \
150
+ --with protobuf \
151
+ --python 3.12 \
152
+ -- \
153
+ --target-repo Mike0021/MiniCPM5-1B-ONNX-Web \
154
+ --export-dtype fp16
155
+ ```
assets/__vite-browser-external-2447137e-BIHI7g3E.js ADDED
@@ -0,0 +1 @@
 
 
1
+ const e={};export{e as default};
assets/addon-fit-DX4qG4td.js ADDED
@@ -0,0 +1 @@
 
 
1
+ var p=2,c=1,d=class{activate(e){this._terminal=e}dispose(){}fit(){let e=this.proposeDimensions();if(!e||!this._terminal||isNaN(e.cols)||isNaN(e.rows))return;let i=this._terminal._core;(this._terminal.rows!==e.rows||this._terminal.cols!==e.cols)&&(i._renderService.clear(),this._terminal.resize(e.cols,e.rows))}proposeDimensions(){if(!this._terminal||!this._terminal.element||!this._terminal.element.parentElement)return;let e=this._terminal._core._renderService.dimensions;if(e.css.cell.width===0||e.css.cell.height===0)return;let i=this._terminal.options.scrollback===0?0:this._terminal.options.overviewRuler?.width||14,s=window.getComputedStyle(this._terminal.element.parentElement),l=parseInt(s.getPropertyValue("height")),o=Math.max(0,parseInt(s.getPropertyValue("width"))),t=window.getComputedStyle(this._terminal.element),r={top:parseInt(t.getPropertyValue("padding-top")),bottom:parseInt(t.getPropertyValue("padding-bottom")),right:parseInt(t.getPropertyValue("padding-right")),left:parseInt(t.getPropertyValue("padding-left"))},a=r.top+r.bottom,n=r.right+r.left,h=l-a,m=o-n-i;return{cols:Math.max(p,Math.floor(m/e.css.cell.width)),rows:Math.max(c,Math.floor(h/e.css.cell.height))}}};export{d as FitAddon};
assets/anthropic-Dm0nVZ1A.js ADDED
The diff for this file is too large to render. See raw diff
 
assets/azure-openai-responses-BGzUURKK.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-CB_zB4U3.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-DBUlz2NN.js";import{b as P}from"./transform-messages-DSNYjDRn.js";import"./hash-DMDecQcg.js";import"./json-parse-cUk3uCki.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=V(r,o,e);let u=x(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}:{},maxRetries:e?.maxRetries??0},{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 V(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 x(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};
assets/client-Sq9UPDIz.js ADDED
The diff for this file is too large to render. See raw diff
 
assets/ghostty-web-C21go6Pk.js ADDED
The diff for this file is too large to render. See raw diff
 
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};
assets/google-kA_fw5EZ.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-tMPbwRUC.js";import{A as G,g as x,c as O,a as A}from"./index-CB_zB4U3.js";import{s as N}from"./sanitize-unicode-B62XMGgN.js";import{b as E}from"./transform-messages-DSNYjDRn.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};
assets/google-shared-tMPbwRUC.js ADDED
The diff for this file is too large to render. See raw diff
 
assets/google-vertex-CxMnHyvQ.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-tMPbwRUC.js";import{A,c as N,a as P}from"./index-CB_zB4U3.js";import{s as U}from"./sanitize-unicode-B62XMGgN.js";import{b as D}from"./transform-messages-DSNYjDRn.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};
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};
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};
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};
assets/index-CB_zB4U3.js ADDED
The diff for this file is too large to render. See raw diff
 
assets/index-NRwwiW4N.css ADDED
@@ -0,0 +1 @@
 
 
1
+ :root{color:#d6deeb;background:#0b0f16;font-family:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,sans-serif}*{box-sizing:border-box}html,body,#app{min-width:320px;min-height:100vh;margin:0}button,select{font:inherit}button{min-height:40px;padding:0 15px;border:0;border-radius:6px;background:#227c70;color:#fff;font-weight:750;cursor:pointer}button:disabled{cursor:wait;opacity:.6}#app{display:grid;grid-template-rows:auto minmax(0,1fr);gap:10px;padding:10px}.runtime-bar{display:flex;align-items:end;justify-content:space-between;gap:16px;min-height:54px;padding:4px 2px 10px;border-bottom:1px solid #263244}h1,h2,p{margin:0}h1{color:#f8fafc;font-size:30px;line-height:1;letter-spacing:0}#model-label{margin-top:5px;color:#9eb1ca;font-size:13px;overflow-wrap:anywhere}.status-stack{display:flex;flex-wrap:wrap;justify-content:end;gap:8px;max-width:760px}.status{min-height:32px;max-width:280px;padding:6px 10px;border:1px solid #344156;border-radius:6px;background:#141b27;color:#cad7ea;font-size:13px;overflow-wrap:anywhere}.terminal-shell{min-height:0;border:1px solid #263244;border-radius:8px;background:#10141c;overflow:hidden}#terminal{width:100%;height:calc(100vh - 86px);min-height:420px;padding:10px}.xterm{height:100%}.xterm-viewport,.xterm-screen{border-radius:0}.model-gate{position:fixed;inset:0;display:grid;place-items:center;padding:18px;background:#04080ea3;z-index:20}.model-gate.hidden{display:none}.model-dialog{display:grid;gap:16px;width:min(560px,100%);padding:22px;border:1px solid #334155;border-radius:8px;background:#f8fafc;color:#172033;box-shadow:0 24px 80px #00000059}.eyebrow{margin-bottom:7px;color:#227c70;font-size:12px;font-weight:850;text-transform:uppercase}.model-dialog h2{color:#172033;font-size:24px;line-height:1.15}.dialog-copy{margin-top:8px;color:#526173;line-height:1.5}.dialog-actions{display:flex;flex-wrap:wrap;gap:10px}#use-test-model{background:#4f5f73}.dialog-options{display:grid;gap:10px;color:#526173}label{display:grid;gap:6px;font-size:13px;font-weight:700}select{width:100%;height:40px;padding:0 10px;border:1px solid #b8c5d4;border-radius:6px;background:#fff;color:#152033}#gate-status{min-height:22px;color:#526173;overflow-wrap:anywhere}@media(max-width:760px){#app{padding:8px}.runtime-bar{align-items:stretch;flex-direction:column}.status-stack{justify-content:start}#terminal{height:calc(100vh - 164px);min-height:360px;padding:8px}}.xterm{cursor:text;position:relative;user-select:none;-ms-user-select:none;-webkit-user-select:none}.xterm.focus,.xterm:focus{outline:none}.xterm .xterm-helpers{position:absolute;top:0;z-index:5}.xterm .xterm-helper-textarea{padding:0;border:0;margin:0;position:absolute;opacity:0;left:-9999em;top:0;width:0;height:0;z-index:-5;white-space:nowrap;overflow:hidden;resize:none}.xterm .composition-view{background:#000;color:#fff;display:none;position:absolute;white-space:nowrap;z-index:1}.xterm .composition-view.active{display:block}.xterm .xterm-viewport{background-color:#000;overflow-y:scroll;cursor:default;position:absolute;inset:0}.xterm .xterm-screen{position:relative}.xterm .xterm-screen canvas{position:absolute;left:0;top:0}.xterm-char-measure-element{display:inline-block;visibility:hidden;position:absolute;top:0;left:-9999em;line-height:normal}.xterm.enable-mouse-events{cursor:default}.xterm.xterm-cursor-pointer,.xterm .xterm-cursor-pointer{cursor:pointer}.xterm.column-select.focus{cursor:crosshair}.xterm .xterm-accessibility:not(.debug),.xterm .xterm-message{position:absolute;inset:0;z-index:10;color:transparent;pointer-events:none}.xterm .xterm-accessibility-tree:not(.debug) *::selection{color:transparent}.xterm .xterm-accessibility-tree{font-family:monospace;-webkit-user-select:text;user-select:text;white-space:pre}.xterm .xterm-accessibility-tree>div{transform-origin:left;width:fit-content}.xterm .live-region{position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden}.xterm-dim{opacity:1!important}.xterm-underline-1{text-decoration:underline}.xterm-underline-2{text-decoration:double underline}.xterm-underline-3{text-decoration:wavy underline}.xterm-underline-4{text-decoration:dotted underline}.xterm-underline-5{text-decoration:dashed underline}.xterm-overline{text-decoration:overline}.xterm-overline.xterm-underline-1{text-decoration:overline underline}.xterm-overline.xterm-underline-2{text-decoration:overline double underline}.xterm-overline.xterm-underline-3{text-decoration:overline wavy underline}.xterm-overline.xterm-underline-4{text-decoration:overline dotted underline}.xterm-overline.xterm-underline-5{text-decoration:overline dashed underline}.xterm-strikethrough{text-decoration:line-through}.xterm-screen .xterm-decoration-container .xterm-decoration{z-index:6;position:absolute}.xterm-screen .xterm-decoration-container .xterm-decoration.xterm-decoration-top-layer{z-index:7}.xterm-decoration-overview-ruler{z-index:8;position:absolute;top:0;right:0;pointer-events:none}.xterm-decoration-top{z-index:2;position:relative}.xterm .xterm-scrollable-element>.scrollbar{cursor:default}.xterm .xterm-scrollable-element>.scrollbar>.scra{cursor:pointer;font-size:11px!important}.xterm .xterm-scrollable-element>.visible{opacity:1;background:#0000;transition:opacity .1s linear;z-index:11}.xterm .xterm-scrollable-element>.invisible{opacity:0;pointer-events:none}.xterm .xterm-scrollable-element>.invisible.fade{transition:opacity .8s linear}.xterm .xterm-scrollable-element>.shadow{position:absolute;display:none}.xterm .xterm-scrollable-element>.shadow.top{display:block;top:0;left:3px;height:3px;width:100%;box-shadow:var(--vscode-scrollbar-shadow, #000) 0 6px 6px -6px inset}.xterm .xterm-scrollable-element>.shadow.left{display:block;top:3px;left:0;height:100%;width:3px;box-shadow:var(--vscode-scrollbar-shadow, #000) 6px 0 6px -6px inset}.xterm .xterm-scrollable-element>.shadow.top-left-corner{display:block;top:0;left:0;height:3px;width:3px}.xterm .xterm-scrollable-element>.shadow.top.left{box-shadow:var(--vscode-scrollbar-shadow, #000) 6px 0 6px -6px inset}
assets/json-parse-cUk3uCki.js ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ import{d as o}from"./index-CB_zB4U3.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};
assets/mistral-mDLdw-bF.js ADDED
The diff for this file is too large to render. See raw diff
 
assets/openai-codex-responses-7plNQja4.js ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import{A as ue,g as Y,a as le}from"./index-CB_zB4U3.js";import{h as z}from"./headers-CgnjaPPL.js";import{c as de}from"./openai-prompt-cache-h_V5PL7P.js";import{c as V,a as fe,p as G}from"./openai-responses-shared-DBUlz2NN.js";import{b as pe}from"./transform-messages-DSNYjDRn.js";import"./hash-DMDecQcg.js";import"./json-parse-cUk3uCki.js";import"./sanitize-unicode-B62XMGgN.js";const $=new Set;function me(e){return $.add(e),()=>{$.delete(e)}}function L(e){return e instanceof Error?e.message||e.name:typeof e=="string"?e:String(e)}function ye(e){if(!(e instanceof Error))return{name:"ThrownValue",message:L(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 be(e,r,t){return{type:e,timestamp:Date.now(),error:ye(r),details:t}}function we(e,r){e.diagnostics=[...e.diagnostics??[],r]}function ge(e){const r=e.filter(o=>o!==void 0);if(r.length===0)return{cleanup:()=>{}};if(r.length===1)return{signal:r[0],cleanup:()=>{}};const t=new AbortController,s=[],n=o=>{t.signal.aborted||t.abort(o.reason)};for(const o of r){if(o.aborted){n(o);break}const a=()=>n(o);o.addEventListener("abort",a,{once:!0}),s.push({signal:o,listener:a})}return{signal:t.signal,cleanup:()=>{for(const{signal:o,listener:a}of s)o.removeEventListener("abort",a)}}}var A={},he=function(e,r){return typeof e=="string"&&/^\.\.?\//.test(e)?e.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i,function(t,s,n,o,a){return s?".js":n&&(!o||!a)?t:n+o+"."+a.toLowerCase()+"js"}):e};let x=null;const Q=e=>import(he(e)),Ee="node:os";typeof process<"u"&&(process.versions?.node||process.versions?.bun)&&Q(Ee).then(e=>{x=e});const Se="https://chatgpt.com/backend-api",ve="https://api.openai.com/auth",ke=0,F=1e3,_e=6e4,U=1e4,Te=15e3,Z=new Set(["openai","openai-codex","opencode"]),xe=1009,Re=new Set(["completed","incomplete","failed","cancelled","queued","in_progress"]);function Ce(e){return/GoUsageLimitError|FreeUsageLimitError|Monthly usage limit reached|available balance|insufficient_quota|out of budget|quota exceeded|billing/i.test(e)}function Ae(e,r){return e===429&&Ce(r)?!1:e===429||e===500||e===502||e===503||e===504?!0:/rate.?limit|overloaded|service.?unavailable|upstream.?connect|connection.?refused/i.test(r)}function Oe(e){const r=e.get("retry-after-ms");if(r!==null){const o=Number(r);if(Number.isFinite(o))return Math.max(0,o)}const t=e.get("retry-after");if(!t)return;const s=Number(t);if(Number.isFinite(s))return Math.max(0,s*1e3);const n=Date.parse(t);if(!Number.isNaN(n))return Math.max(0,n-Date.now())}function We(e,r){const t=r?.maxRetryDelayMs??_e;return t>0?Math.min(e,t):e}function J(e,r){return new Promise((t,s)=>{if(r?.aborted){s(new Error("Request was aborted"));return}const n=setTimeout(t,e);r?.addEventListener("abort",()=>{clearTimeout(n),s(new Error("Request was aborted"))})})}function H(e){if(e!==void 0){if(!Number.isFinite(e)||e<0)throw new Error(`Invalid timeoutMs: ${String(e)}`);return Math.floor(e)}}function Ie(){const e=new AbortController;let r;const t=setTimeout(()=>{r=new Error(`Codex SSE response headers timed out after ${U}ms`),e.abort(r)},U);return{signal:e.signal,clear:()=>clearTimeout(t),error:()=>r}}const Le=(e,r,t)=>{const s=new ue;return(async()=>{const n={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 o=t?.apiKey||Y(e.provider)||"";if(!o)throw new Error(`No API key for provider: ${e.provider}`);const a=st(o);let l=De(e,r,t);const i=await t?.onPayload?.(l,e);i!==void 0&&(l=i);const c=t?.sessionId||nt(),p=at(e.headers,t?.headers,a,o,t?.sessionId),h=it(e.headers,t?.headers,a,o,c),E=JSON.stringify(l),d=H(t?.timeoutMs),g=H(t?.websocketConnectTimeoutMs),m=t?.transport||"auto",y=m!=="sse"&&se(t?.sessionId);if(y&&K(t?.sessionId),m!=="sse"&&!y){let b=!1;try{if(await rt(qe(e.baseUrl),l,h,n,s,e,()=>{b=!0},d,g,t),t?.signal?.aborted)throw new Error("Request was aborted");s.push({type:"done",reason:n.stopReason,message:n}),s.end();return}catch(w){if(t?.signal?.aborted||Be(w)||(we(n,be("provider_transport_failure",w,{configuredTransport:m,fallbackTransport:b?void 0:"sse",eventsEmitted:b,phase:b?"after_message_stream_start":"before_message_stream_start",requestBytes:new TextEncoder().encode(E).byteLength})),He(t?.sessionId,w),b))throw w;K(t?.sessionId)}}let u,f;const v=t?.maxRetries??ke;for(let b=0;b<=v;b++){if(t?.signal?.aborted)throw new Error("Request was aborted");try{const w=Ie(),_=ge([t?.signal,w.signal]);try{u=await fetch(re(e.baseUrl),{method:"POST",headers:p,body:E,signal:_.signal})}catch(T){const C=w.error();throw C&&!t?.signal?.aborted?C:T}finally{_.cleanup(),w.clear()}if(await t?.onResponse?.({status:u.status,headers:z(u.headers)},e),u.ok)break;const B=await u.text();if(b<v&&Ae(u.status,B)){const T=Oe(u.headers),C=T===void 0?F*2**b:u.status===429?We(T,t):T;await J(C,t?.signal);continue}const ce=new Response(B,{status:u.status,statusText:u.statusText}),P=await ot(ce);throw new Error(P.friendlyMessage||P.message)}catch(w){if(w instanceof Error&&(w.name==="AbortError"||w.message==="Request was aborted"))throw new Error("Request was aborted");if(f=w instanceof Error?w:new Error(String(w)),b<v&&!f.message.includes("usage limit")){const _=F*2**b;await J(_,t?.signal);continue}throw f}}if(!u?.ok)throw f??new Error("Failed after retries");if(!u.body)throw new Error("No response body");if(s.push({type:"start",partial:n}),await Ne(u,n,s,e,t),t?.signal?.aborted)throw new Error("Request was aborted");s.push({type:"done",reason:n.stopReason,message:n}),s.end()}catch(o){for(const a of n.content)delete a.partialJson;n.stopReason=t?.signal?.aborted?"aborted":"error",n.errorMessage=o instanceof Error?o.message:String(o),s.push({type:"error",reason:n.stopReason,error:n}),s.end()}})(),s},bt=(e,r,t)=>{const s=t?.apiKey||Y(e.provider);if(!s)throw new Error(`No API key for provider: ${e.provider}`);const n=pe(e,t,s),o=t?.reasoning?le(e,t.reasoning):void 0;return Le(e,r,{...n,reasoningEffort:o==="off"?void 0:o})};function De(e,r,t){const s=V(e,r,Z,{includeSystemPrompt:!1}),n={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:de(t?.sessionId),tool_choice:"auto",parallel_tool_calls:!0};if(t?.temperature!==void 0&&(n.temperature=t.temperature),t?.serviceTier!==void 0&&(n.service_tier=t.serviceTier),r.tools&&r.tools.length>0&&(n.tools=fe(r.tools,{strict:null})),t?.reasoningEffort!==void 0){const o=t.reasoningEffort==="none"?e.thinkingLevelMap?.off??"none":e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort;o!==null&&(n.reasoning={effort:o,summary:t.reasoningSummary??"auto"})}return n}function Me(e,r){switch(r){case"flex":return .5;case"priority":return e.id==="gpt-5.5"?2.5:2;default:return 1}}function ee(e,r,t){const s=Me(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 te(e,r){return e==="default"&&(r==="flex"||r==="priority")?r:e??r}function re(e){const t=(e&&e.trim().length>0?e:Se).replace(/\/+$/,"");return t.endsWith("/codex/responses")?t:t.endsWith("/codex")?`${t}/responses`:`${t}/codex/responses`}function qe(e){const r=new URL(re(e));return r.protocol==="https:"&&(r.protocol="wss:"),r.protocol==="http:"&&(r.protocol="ws:"),r.toString()}async function Ne(e,r,t,s,n){await G(oe($e(e)),r,t,s,{serviceTier:n?.serviceTier,resolveServiceTier:te,applyServiceTierPricing:(o,a)=>ee(o,a,s)})}class M 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 Be(e){return e instanceof M||e instanceof q}async function*oe(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||"",n=r.message||"";throw new M(`Codex error: ${n||s||JSON.stringify(r)}`,{code:s||void 0,payload:r})}if(t==="response.failed"){const s=r.response,n=s?.error?.code,o=s?.error?.message;throw new M(o||"Codex response failed",{code:n,payload:r})}if(t==="response.done"||t==="response.completed"||t==="response.incomplete"){const s=r.response,n=s&&{...s,status:Pe(s.status)};yield{...r,type:"response.completed",response:n};return}yield r}}}function Pe(e){if(typeof e=="string")return Re.has(e)?e:void 0}async function*$e(e){if(!e.body)return;const r=e.body.getReader(),t=new TextDecoder;let s="";try{for(;;){const{done:n,value:o}=await r.read();if(n)break;s+=t.decode(o,{stream:!0});let a=s.indexOf(`
2
+
3
+ `);for(;a!==-1;){const l=s.slice(0,a);s=s.slice(a+2);const i=l.split(`
4
+ `).filter(c=>c.startsWith("data:")).map(c=>c.slice(5).trim());if(i.length>0){const c=i.join(`
5
+ `).trim();if(c&&c!=="[DONE]")try{yield JSON.parse(c)}catch(p){throw new q(`Invalid Codex SSE JSON: ${L(p)}`,{cause:p,payload:c})}}a=s.indexOf(`
6
+
7
+ `)}}}finally{try{await r.cancel()}catch{}try{r.releaseLock()}catch{}}}const Fe="responses_websockets=2026-02-06",Ue=300*1e3,S=new Map,R=new Map,I=new Set;function N(e){let r=R.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},R.set(e,r)),r}function wt(e){const r=R.get(e);return r?{...r}:void 0}function gt(e){if(e){R.delete(e),I.delete(e);return}R.clear(),I.clear()}function Je(e){const r=t=>{t.idleTimer&&clearTimeout(t.idleTimer),k(t.socket,1e3,"debug_close")};if(e){const t=S.get(e);t&&r(t),S.delete(e);return}for(const t of S.values())r(t);S.clear()}me(Je);function se(e){return e?I.has(e):!1}function K(e){if(!e)return;const r=N(e);r.sseFallbacks++,r.websocketFallbackActive=se(e)}function He(e,r){if(!e)return;I.add(e);const t=N(e);t.websocketFailures++,t.lastWebSocketError=L(r),t.websocketFallbackActive=!0}let O=null;async function Ke(){if(O)return O;if(process?.versions?.bun&&(A.HTTP_PROXY||A.HTTPS_PROXY||A.http_proxy||A.https_proxy)){const t=(await Q("proxy-from-env")).getProxyForUrl;return O=class extends WebSocket{constructor(s,n){let o={};Array.isArray(n)||typeof n=="string"?o={protocols:n}:o={...n};const a=t(s.toString().replace(/^wss:/,"https:").replace(/^ws:/,"http:"));super(s,{...o,...a?{proxy:a}:{}})}},O}const e=globalThis.WebSocket;return typeof e!="function"?null:e}class je 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 Xe(e){const r=e.readyState;return typeof r=="number"?r:void 0}function W(e){const r=Xe(e);return r===void 0||r===1}function k(e,r=1e3,t="done"){try{e.close(r,t)}catch{}}function j(e,r){r.idleTimer&&clearTimeout(r.idleTimer),r.idleTimer=setTimeout(()=>{r.busy||(k(r.socket,1e3,"idle_timeout"),S.delete(e))},Ue)}async function D(e,r,t,s=Te){const n=await Ke();if(!n)throw new Error("WebSocket transport is not available in this runtime");const o=z(r);return delete o["OpenAI-Beta"],new Promise((a,l)=>{let i=!1,c,p;try{p=new n(e,{headers:o})}catch(u){l(u instanceof Error?u:new Error(String(u)));return}const h=()=>{c&&(clearTimeout(c),c=void 0),p.removeEventListener("open",d),p.removeEventListener("error",g),p.removeEventListener("close",m),t?.removeEventListener("abort",y)},E=(u,f)=>{i||(i=!0,h(),f&&k(p,1e3,f),l(u))},d=()=>{i||(i=!0,h(),a(p))},g=u=>{E(ne(u))},m=u=>{E(ae(u))},y=()=>{E(new Error("Request was aborted"),"aborted")};p.addEventListener("open",d),p.addEventListener("error",g),p.addEventListener("close",m),t?.addEventListener("abort",y),s>0&&(c=setTimeout(()=>{E(new Error(`WebSocket connect timeout after ${s}ms`),"connect_timeout")},s)),t?.aborted&&y()})}async function Ye(e,r,t,s,n){if(!t){const i=await D(e,r,s,n);return{socket:i,reused:!1,release:()=>k(i)}}const o=S.get(t);if(o){if(o.idleTimer&&(clearTimeout(o.idleTimer),o.idleTimer=void 0),!o.busy&&W(o.socket))return o.busy=!0,{socket:o.socket,entry:o,reused:!0,release:({keep:i}={})=>{if(!i||!W(o.socket)){k(o.socket),S.delete(t);return}o.busy=!1,j(t,o)}};if(o.busy){const i=await D(e,r,s,n);return{socket:i,reused:!1,release:()=>{k(i)}}}W(o.socket)||(k(o.socket),S.delete(t))}const a=await D(e,r,s,n),l={socket:a,busy:!0};return S.set(t,l),{socket:a,entry:l,reused:!1,release:({keep:i}={})=>{if(!i||!W(l.socket)){k(l.socket),l.idleTimer&&clearTimeout(l.idleTimer),S.get(t)===l&&S.delete(t);return}l.busy=!1,j(t,l)}}}function ne(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 ae(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,n=typeof r=="number"?` ${r}`:"";let o=typeof t=="string"&&t.length>0?` ${t}`:"";return!o&&r===xe&&(o=" message too big"),new je(`WebSocket closed${n}${o}`.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 ze(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*Ve(e,r,t){const s=[];let n=null,o=!1,a=null,l=!1;const i=()=>{if(!n)return;const d=n;n=null,d()},c=d=>{(async()=>{let g=null;try{if(!d||typeof d!="object"||!("data"in d)||(g=await ze(d.data),!g))return;const m=JSON.parse(g),y=typeof m.type=="string"?m.type:"";(y==="response.completed"||y==="response.done"||y==="response.incomplete")&&(l=!0,o=!0),s.push(m),i()}catch(m){a=new q(`Invalid Codex WebSocket JSON: ${L(m)}`,{cause:m,payload:g}),o=!0,i()}})()},p=d=>{a=ne(d),o=!0,i()},h=d=>{if(l){o=!0,i();return}a||(a=ae(d)),o=!0,i()},E=()=>{a=new Error("Request was aborted"),o=!0,i()};e.addEventListener("message",c),e.addEventListener("error",p),e.addEventListener("close",h),r?.addEventListener("abort",E);try{for(;;){if(r?.aborted)throw new Error("Request was aborted");if(s.length>0){yield s.shift();continue}if(o)break;let d;await new Promise((g,m)=>{n=g,t!==void 0&&t>0&&(d=setTimeout(()=>{const y=new Error(`WebSocket idle timeout after ${t}ms`);a=y,o=!0,n=null,k(e,1e3,"idle_timeout"),m(y)},t))}).finally(()=>{d&&clearTimeout(d)})}if(a)throw a;if(!l)throw new Error("WebSocket stream closed before response.completed")}finally{e.removeEventListener("message",c),e.removeEventListener("error",p),e.removeEventListener("close",h),r?.removeEventListener("abort",E)}}function X(e){const{input:r,previous_response_id:t,...s}=e;return s}function Ge(e,r){return JSON.stringify(e??[])===JSON.stringify(r??[])}function Qe(e,r){return JSON.stringify(X(e))===JSON.stringify(X(r))}function Ze(e,r){if(!Qe(e,r.lastRequestBody))return;const t=e.input??[],s=[...r.lastRequestBody.input??[],...r.lastResponseItems];if(t.length<s.length)return;const n=t.slice(0,s.length);if(Ge(n,s))return t.slice(s.length)}function et(e,r){const t=e.continuation;if(!t)return r;const s=Ze(r,t);return!s||!t.lastResponseId?(e.continuation=void 0,r):{...r,previous_response_id:t.lastResponseId,input:s}}async function*tt(e,r,t,s){let n=!1;for await(const o of e)n||(n=!0,s(),t.push({type:"start",partial:r})),yield o}async function rt(e,r,t,s,n,o,a,l,i,c){const{socket:p,entry:h,reused:E,release:d}=await Ye(e,t,c?.sessionId,c?.signal,i);let g=!0;const m=c?.transport==="websocket-cached"||c?.transport==="auto",y=r,u=m&&h?et(h,y):y,f=c?.sessionId?N(c.sessionId):void 0;f&&(f.requests++,E?f.connectionsReused++:f.connectionsCreated++,m&&f.cachedContextRequests++,u.store===!0&&f.storeTrueRequests++,f.lastInputItems=u.input?.length??0,u.previous_response_id?(f.deltaRequests++,f.lastDeltaInputItems=u.input?.length??0,f.lastPreviousResponseId=u.previous_response_id):(f.fullContextRequests++,f.lastDeltaInputItems=void 0,f.lastPreviousResponseId=void 0));try{if(p.send(JSON.stringify({type:"response.create",...u})),await G(tt(oe(Ve(p,c?.signal,l)),s,n,a),s,n,o,{serviceTier:c?.serviceTier,resolveServiceTier:te,applyServiceTierPricing:(v,b)=>ee(v,b,o)}),c?.signal?.aborted)g=!1;else if(m&&h&&s.responseId){const v=V(o,{messages:[s]},Z,{includeSystemPrompt:!1}).filter(b=>b.type!=="function_call_output");h.continuation={lastRequestBody:y,lastResponseId:s.responseId,lastResponseItems:v}}}catch(v){throw h&&(h.continuation=void 0),g=!1,v}finally{d({keep:g})}}async function ot(e){const r=await e.text();let t=r||e.statusText||"Request failed",s;try{const o=JSON.parse(r)?.error;if(o){const a=o.code||o.type||"";if(/usage_limit_reached|usage_not_included|rate_limit_exceeded/i.test(a)||e.status===429){const l=o.plan_type?` (${o.plan_type.toLowerCase()} plan)`:"",i=o.resets_at?Math.max(0,Math.round((o.resets_at*1e3-Date.now())/6e4)):void 0,c=i!==void 0?` Try again in ~${i} min.`:"";s=`You have hit your ChatGPT usage limit${l}.${c}`.trim()}t=o.message||s||t}}catch{}return{message:t,friendlyMessage:s}}function st(e){try{const r=e.split(".");if(r.length!==3)throw new Error("Invalid token");const s=JSON.parse(atob(r[1]))?.[ve]?.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 nt(){return typeof globalThis.crypto?.randomUUID=="function"?globalThis.crypto.randomUUID():`codex_${Date.now()}_${Math.random().toString(36).slice(2,10)}`}function ie(e,r,t,s){const n=new Headers(e);for(const[a,l]of Object.entries(r||{}))n.set(a,l);n.set("Authorization",`Bearer ${s}`),n.set("chatgpt-account-id",t),n.set("originator","pi");const o=x?`pi (${x.platform()} ${x.release()}; ${x.arch()})`:"pi (browser)";return n.set("User-Agent",o),n}function at(e,r,t,s,n){const o=ie(e,r,t,s);return o.set("OpenAI-Beta","responses=experimental"),o.set("accept","text/event-stream"),o.set("content-type","application/json"),n&&(o.set("session-id",n),o.set("x-client-request-id",n)),o}function it(e,r,t,s,n){const o=ie(e,r,t,s);return o.delete("accept"),o.delete("content-type"),o.delete("OpenAI-Beta"),o.delete("openai-beta"),o.set("OpenAI-Beta",Fe),o.set("x-client-request-id",n),o.set("session-id",n),o}export{Je as closeOpenAICodexWebSocketSessions,wt as getOpenAICodexWebSocketDebugStats,gt as resetOpenAICodexWebSocketDebugStats,Le as streamOpenAICodexResponses,bt as streamSimpleOpenAICodexResponses};
assets/openai-completions-CTz74GS-.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-CB_zB4U3.js";import{h as J}from"./headers-CgnjaPPL.js";import{a as P}from"./json-parse-cUk3uCki.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-DSNYjDRn.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 ie=(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=ae(e,n,c,t?.headers,i,u);let h=le(e,n,t,u,o);const k=await t?.onPayload?.(h,e);k!==void 0&&(h=k);const R={...t?.signal?{signal:t.signal}:{},...t?.timeoutMs!==void 0?{timeout:t.timeoutMs}:{},maxRetries:t?.maxRetries??0},{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,y=!1;const w=new Map,I=new Map,E=r.content,A=a=>E.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:""},E.push(v),s.push({type:"text_start",contentIndex:A(v),partial:r})),v),H=a=>(l||(l={type:"thinking",thinking:"",thinkingSignature:a},E.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?w.get(f):void 0;return!d&&a.id&&(d=I.get(a.id)),d||(d={type:"toolCall",id:a.id||"",name:a.function?.name||"",arguments:{},partialArgs:"",streamIndex:f},f!==void 0&&w.set(f,d),a.id&&I.set(a.id,d),E.push(d),s.push({type:"toolcall_start",contentIndex:A(d),partial:r})),f!==void 0&&d.streamIndex===void 0&&(d.streamIndex=f,w.set(f,d)),a.id&&I.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=ye(f.finish_reason);r.stopReason=d.stopReason,d.errorMessage&&(r.errorMessage=d.errorMessage),y=!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 M=null;for(const g of d){const m=O[g];if(typeof m=="string"&&m.length>0){M=g;break}}if(M){const g=O[M];if(typeof g=="string"&&g.length>0){const m=e.provider==="opencode-go"&&M==="reasoning"?"reasoning_content":M,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,I.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 S=f.delta.reasoning_details;if(S&&Array.isArray(S)){for(const g of S)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 E)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(!y)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},Ie=(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 ie(e,n,{...r,reasoningEffort:u,toolChoice:o})};function ae(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=ke(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(s.thinkingFormat==="string-thinking"&&e.reasoning){const i=o;t?.reasoningEffort?i.thinking=e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort:e.thinkingLevelMap?.off!==null&&(i.thinking=e.thinkingLevelMap?.off??"none")}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)})),k=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(y=>C(y.thinking)).join(`
3
+
4
+ `);p.content=[{type:"text",text:l},...h]}else{k.length>0&&(p.content=k);let l=R[0].thinkingSignature;e.provider==="opencode-go"&&l==="reasoning"&&(l="reasoning_content"),l&&l.length>0&&(p[l]=R.map(y=>y.thinking).join(`
5
+ `))}else k.length>0&&(p.content=k);const T=i.content.filter(oe);if(T.length>0){p.tool_calls=T.map(y=>({id:y.id,type:"function",function:{name:y.name,arguments:JSON.stringify(y.arguments)}}));const l=T.filter(y=>y.thoughtSignature).map(y=>{try{return JSON.parse(y.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 k=c[h],R=k.content.filter(F).map(l=>l.text).join(`
6
+ `),T=k.content.some(l=>l.type==="image"),_=R.length>0,v={role:"tool",content:C(_?R:"(see attached image)"),tool_call_id:k.toolCallId};if(t.requiresToolResultName&&k.toolName&&(v.name=k.toolName),s.push(v),T&&e.input.includes("image"))for(const l of k.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 ke(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 ye(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"),k=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:k,thinkingFormat:k?"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,ie as streamOpenAICompletions,Ie as streamSimpleOpenAICompletions};
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};
assets/openai-responses-CW5o7IM-.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-CB_zB4U3.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-DBUlz2NN.js";import{b as L}from"./transform-messages-DSNYjDRn.js";import"./hash-DMDecQcg.js";import"./json-parse-cUk3uCki.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}:{},maxRetries:r?.maxRetries??0},{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};
assets/openai-responses-shared-DBUlz2NN.js ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import{c as $}from"./index-CB_zB4U3.js";import{s as S}from"./hash-DMDecQcg.js";import{a as k}from"./json-parse-cUk3uCki.js";import{s as x}from"./sanitize-unicode-B62XMGgN.js";import{t as T}from"./transform-messages-DSNYjDRn.js";function C(r,t){const l={v:1,id:r};return t&&(l.phase=t),JSON.stringify(l)}function J(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 W(r,t,l,h){const u=[],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_${S(s)}`;return o.length>64?o.slice(0,64):o},I=(s,o,c)=>{if(!l.has(r.provider)||!s.includes("|"))return i(s);const[_,y]=s.split("|"),f=i(_);let m=c.provider!==r.provider||c.api!==r.api?n(y):i(y);return m.startsWith("fc_")||(m=i(`fc_${m}`)),`${f}|${m}`},d=T(t.messages,r,I);if((h?.includeSystemPrompt??!0)&&t.systemPrompt){const s=r.reasoning?"developer":"system";u.push({role:s,content:x(t.systemPrompt)})}let e=0;for(const s of d){if(s.role==="user")if(typeof s.content=="string")u.push({role:"user",content:[{type:"input_text",text:x(s.content)}]});else{const o=s.content.map(c=>c.type==="text"?{type:"input_text",text:x(c.text)}:{type:"input_image",detail:"auto",image_url:`data:${c.mimeType};base64,${c.data}`});if(o.length===0)continue;u.push({role:"user",content:o})}else if(s.role==="assistant"){const o=[],c=s,_=c.model!==r.id&&c.provider===r.provider&&c.api===r.api;let y=0;for(const f of s.content)if(f.type==="thinking"){if(f.thinkingSignature){const p=JSON.parse(f.thinkingSignature);o.push(p)}}else if(f.type==="text"){const p=f,m=J(p.textSignature),v=y===0?`msg_pi_${e}`:`msg_pi_${e}_${y}`;y++;let g=m?.id;g?g.length>64&&(g=`msg_${S(g)}`):g=v,o.push({type:"message",role:"assistant",content:[{type:"output_text",text:x(p.text),annotations:[]}],status:"completed",id:g,phase:m?.phase})}else if(f.type==="toolCall"){const p=f,[m,v]=p.id.split("|");let g=v;_&&g?.startsWith("fc_")&&(g=void 0),o.push({type:"function_call",id:g,call_id:m,name:p.name,arguments:JSON.stringify(p.arguments)})}if(o.length===0)continue;u.push(...o)}else if(s.role==="toolResult"){const o=s.content.filter(p=>p.type==="text").map(p=>p.text).join(`
2
+ `),c=s.content.some(p=>p.type==="image"),_=o.length>0,[y]=s.toolCallId.split("|");let f;if(c&&r.input.includes("image")){const p=[];_&&p.push({type:"input_text",text:x(o)});for(const m of s.content)m.type==="image"&&p.push({type:"input_image",detail:"auto",image_url:`data:${m.mimeType};base64,${m.data}`});f=p}else f=x(_?o:"(see attached image)");u.push({type:"function_call_output",call_id:y,output:f})}e++}return u}function N(r,t){const l=t?.strict===void 0?!1:t.strict;return r.map(h=>({type:"function",name:h.name,description:h.description,parameters:h.parameters,strict:l}))}async function O(r,t,l,h,u){let i=null,n=null;const I=t.content,d=()=>I.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=k(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=k(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(c=>c.text).join(`
9
+
10
+ `)||"",o=e.content?.map(c=>c.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=C(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?k(n.partialJson):k(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($(h,t.usage),u?.applyServiceTierPricing){const s=u.resolveServiceTier?u.resolveServiceTier(e?.service_tier,u.serviceTier):e?.service_tier??u.serviceTier;u.applyServiceTierPricing(t.usage,s)}t.stopReason=P(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 P(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{N as a,W as c,O as p};
assets/openrouter-DE7amXV2.js ADDED
@@ -0,0 +1 @@
 
 
1
+ import{O as y}from"./client-Sq9UPDIz.js";import{g as R}from"./index-CB_zB4U3.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}:{},maxRetries:t?.maxRetries??0},{data:l,response:d}=await c.chat.completions.create(n,o).withResponse();await t?.onResponse?.({status:d.status,headers:x(d.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 g of p.message.images??[]){const f=typeof g.image_url=="string"?g.image_url:g.image_url?.url;if(!f?.startsWith("data:"))continue;const h=f.match(/^data:([^;]+);base64,(.+)$/);h&&a.output.push({type:"image",mimeType:h[1],data:h[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};
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
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};
assets/transform-messages-DSNYjDRn.js ADDED
@@ -0,0 +1 @@
 
 
1
+ function w(a,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,websocketConnectTimeoutMs:t?.websocketConnectTimeoutMs,maxRetries:t?.maxRetries,maxRetryDelayMs:t?.maxRetryDelayMs,metadata:t?.metadata}}function R(a){return a==="xhigh"?"high":a}function S(a,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=a===void 0?t:Math.min(a+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(a,t){const n=[];let l=!1;for(const h of a){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(a,t){return t.input.includes("image")?a:a.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 O(a,t,n){const l=new Map,m=C(a,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(o=>{if(o.type==="thinking")return o.redacted?i?o:[]:i&&o.thinkingSignature?o:!o.thinking||o.thinking.trim()===""?[]:i?o:{type:"text",text:o.thinking};if(o.type==="text")return i?o:{type:"text",text:o.text};if(o.type==="toolCall"){const c=o;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 o});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(o=>o.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{S as a,w as b,O as t};
assets/xterm-B-qIQCd3.js ADDED
The diff for this file is too large to render. See raw diff
 
index.html CHANGED
@@ -1,19 +1,55 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>Pi CLI Web</title>
7
+ <script type="module" crossorigin src="/assets/index-CB_zB4U3.js"></script>
8
+ <link rel="stylesheet" crossorigin href="/assets/index-NRwwiW4N.css">
9
+ </head>
10
+ <body>
11
+ <main id="app">
12
+ <header class="runtime-bar" aria-label="Runtime status">
13
+ <div>
14
+ <h1>pi</h1>
15
+ <p id="model-label"></p>
16
+ </div>
17
+ <div class="status-stack">
18
+ <span class="status" id="status">Idle</span>
19
+ <span class="status" id="model-status">Model idle</span>
20
+ <span class="status" id="sandbox-status">Sandbox idle</span>
21
+ </div>
22
+ </header>
23
+
24
+ <section class="terminal-shell" aria-label="Pi terminal">
25
+ <div id="terminal"></div>
26
+ </section>
27
+
28
+ <section class="model-gate" id="model-gate" aria-labelledby="model-gate-title" aria-modal="true" role="dialog">
29
+ <div class="model-dialog">
30
+ <div>
31
+ <p class="eyebrow">Local model setup</p>
32
+ <h2 id="model-gate-title">Download the selected model to this browser?</h2>
33
+ <p class="dialog-copy">
34
+ This static web port runs pi with Transformers.js and WebContainers. The model download stays in browser storage when possible.
35
+ </p>
36
+ </div>
37
+ <div class="dialog-actions">
38
+ <button id="confirm-load-model" type="button">Download Model</button>
39
+ <button id="use-test-model" type="button">Use Test Model</button>
40
+ </div>
41
+ <div class="dialog-options">
42
+ <label>
43
+ Device
44
+ <select id="gate-device">
45
+ <option value="webgpu">WebGPU</option>
46
+ <option value="wasm">WASM</option>
47
+ </select>
48
+ </label>
49
+ <p id="gate-status">Ready.</p>
50
+ </div>
51
+ </div>
52
+ </section>
53
+ </main>
54
+ </body>
55
  </html>
style.css DELETED
@@ -1,28 +0,0 @@
1
- body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
4
- }
5
-
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
9
- }
10
-
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
16
- }
17
-
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
- }
25
-
26
- .card p:last-child {
27
- margin-bottom: 0;
28
- }