Spaces:
Configuration error
Configuration error
Increase tested token budget defaults
Browse files- README.md +7 -1
- dist/assets/anthropic-DOBQqji-.js +0 -0
- dist/assets/azure-openai-responses-B54Va28a.js +1 -0
- dist/assets/google-Bj49APZU.js +1 -0
- dist/assets/google-vertex-BHrrHdVi.js +1 -0
- dist/assets/index-BGwNxTBx.js +0 -0
- dist/assets/json-parse-CyZFb4VN.js +2 -0
- dist/assets/mistral-BKzj2GJS.js +0 -0
- dist/assets/openai-codex-responses-C4HrSSHd.js +7 -0
- dist/assets/openai-completions-ClwNjci9.js +6 -0
- dist/assets/openai-responses-Cl3X-tw0.js +1 -0
- dist/assets/openai-responses-shared-CbKiZ_Nn.js +12 -0
- dist/assets/openrouter-BdCuhyTj.js +1 -0
- dist/index.html +2 -2
- index.html +1 -1
- package.json +1 -0
- scripts/probe_token_budgets.mjs +79 -0
- scripts/smoke_local_model_web_agent.mjs +3 -2
- src/piAgent.js +4 -6
README.md
CHANGED
|
@@ -40,7 +40,7 @@ The app uses:
|
|
| 40 |
- `@huggingface/transformers` with `Mike0021/MiniCPM5-1B-ONNX-Web` for the local browser model.
|
| 41 |
- `@webcontainer/api` for the client-only sandbox with a virtual filesystem and browser-contained Node.js processes.
|
| 42 |
|
| 43 |
-
Vite serves the app with COOP/COEP headers and boots WebContainers with `coep: "credentialless"`. The deterministic test model is available at `http://localhost:5173/?mode=mock&device=wasm` for fast harness and sandbox smoke tests without downloading the full ONNX model.
|
| 44 |
|
| 45 |
The Static Space uses the same isolation policy through `custom_headers` in this README frontmatter. The app is built with `npm run build` and the generated `dist/` directory is uploaded to the Space.
|
| 46 |
|
|
@@ -64,6 +64,12 @@ This downloads/loads the q4 ONNX artifact in Chromium, runs the same pi/WebConta
|
|
| 64 |
|
| 65 |
The sandbox can also install and use Node packages through the same `run_command` tool, for example `npm install is-number@7.0.0` followed by `node check-package.mjs`.
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
## Verify the Published Artifact
|
| 68 |
|
| 69 |
```bash
|
|
|
|
| 40 |
- `@huggingface/transformers` with `Mike0021/MiniCPM5-1B-ONNX-Web` for the local browser model.
|
| 41 |
- `@webcontainer/api` for the client-only sandbox with a virtual filesystem and browser-contained Node.js processes.
|
| 42 |
|
| 43 |
+
Vite serves the app with COOP/COEP headers and boots WebContainers with `coep: "credentialless"`. The deterministic test model is available at `http://localhost:5173/?mode=mock&device=wasm` for fast harness and sandbox smoke tests without downloading the full ONNX model. The local model defaults to a tested 512-token generation budget in WASM mode and the UI allows budgets up to 2048.
|
| 44 |
|
| 45 |
The Static Space uses the same isolation policy through `custom_headers` in this README frontmatter. The app is built with `npm run build` and the generated `dist/` directory is uploaded to the Space.
|
| 46 |
|
|
|
|
| 64 |
|
| 65 |
The sandbox can also install and use Node packages through the same `run_command` tool, for example `npm install is-number@7.0.0` followed by `node check-package.mjs`.
|
| 66 |
|
| 67 |
+
To probe larger browser generation budgets:
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
TOKEN_BUDGETS=512,1024,2048 npm run probe:tokens
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
## Verify the Published Artifact
|
| 74 |
|
| 75 |
```bash
|
dist/assets/anthropic-DOBQqji-.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dist/assets/azure-openai-responses-B54Va28a.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-BGwNxTBx.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-CbKiZ_Nn.js";import{b as P}from"./transform-messages-Bxe2XUr4.js";import"./hash-DMDecQcg.js";import"./json-parse-CyZFb4VN.js";import"./sanitize-unicode-B62XMGgN.js";var w={};class U extends h{constructor({baseURL:t=m("OPENAI_BASE_URL"),apiKey:e=m("AZURE_OPENAI_API_KEY"),apiVersion:s=m("OPENAI_API_VERSION"),endpoint:n,deployment:a,azureADTokenProvider:o,dangerouslyAllowBrowser:i,...u}={}){if(!s)throw new c("The OPENAI_API_VERSION environment variable is missing or empty; either provide it, or instantiate the AzureOpenAI client with an apiVersion option, like new AzureOpenAI({ apiVersion: 'My API Version' }).");if(typeof o=="function"&&(i=!0),!o&&!e)throw new c("Missing credentials. Please pass one of `apiKey` and `azureADTokenProvider`, or set the `AZURE_OPENAI_API_KEY` environment variable.");if(o&&e)throw new c("The `apiKey` and `azureADTokenProvider` arguments are mutually exclusive; only one can be passed at a time.");if(u.defaultQuery={...u.defaultQuery,"api-version":s},t){if(n)throw new c("baseURL and endpoint are mutually exclusive")}else{if(n||(n=w.AZURE_OPENAI_ENDPOINT),!n)throw new c("Must provide one of the `baseURL` or `endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable");t=`${n}/openai`}super({apiKey:o??e,baseURL:t,...u,...i!==void 0?{dangerouslyAllowBrowser:i}:{}}),this.apiVersion="",this.apiVersion=s,this.deploymentName=a}async buildRequest(t,e={}){if(z.has(t.path)&&t.method==="post"&&t.body!==void 0){if(!_(t.body))throw new Error("Expected request body to be an object");const s=this.deploymentName||t.body.model||t.__metadata?.model;s!==void 0&&!this.baseURL.includes("/deployments")&&(t.path=`/deployments/${s}${t.path}`)}return super.buildRequest(t,e)}async authHeaders(t){return typeof this._options.apiKey=="string"?R([{"api-key":this.apiKey}]):super.authHeaders(t)}}const z=new Set(["/completions","/chat/completions","/embeddings","/audio/transcriptions","/audio/translations","/audio/speech","/images/generations","/batches","/images/edits"]);var p={};const S="v1",M=new Set(["openai","openai-codex","opencode","azure-openai-responses"]);function L(r){const t=new Map;if(!r)return t;for(const e of r.split(",")){const s=e.trim();if(!s)continue;const[n,a]=s.split("=",2);!n||!a||t.set(n.trim(),a.trim())}return t}function Z(r,t){return t?.azureDeploymentName?t.azureDeploymentName:L(p.AZURE_OPENAI_DEPLOYMENT_NAME_MAP).get(r.id)||r.id}function k(r){if(r instanceof Error){const t=r.status,e=typeof t=="number"?t:void 0;return e!==void 0?`Azure OpenAI API error (${e}): ${r.message}`:r.message}try{return JSON.stringify(r)}catch{return String(r)}}const D=(r,t,e)=>{const s=new y;return(async()=>{const n=Z(r,e),a={role:"assistant",content:[],api:"azure-openai-responses",provider:r.provider,model:r.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const o=e?.apiKey||l(r.provider)||"",i=x(r,o,e);let u=V(r,t,e,n);const d=await e?.onPayload?.(u,r);d!==void 0&&(u=d);const A={...e?.signal?{signal:e.signal}:{},...e?.timeoutMs!==void 0?{timeout:e.timeoutMs}:{},...e?.maxRetries!==void 0?{maxRetries:e.maxRetries}:{}},{data:E,response:f}=await i.responses.create(u,A).withResponse();if(await e?.onResponse?.({status:f.status,headers:g(f.headers)},r),s.push({type:"start",partial:a}),await O(E,a,s,r),e?.signal?.aborted)throw new Error("Request was aborted");if(a.stopReason==="aborted"||a.stopReason==="error")throw new Error("An unknown error occurred");s.push({type:"done",reason:a.stopReason,message:a}),s.end()}catch(o){for(const i of a.content)delete i.index,delete i.partialJson;a.stopReason=e?.signal?.aborted?"aborted":"error",a.errorMessage=k(o),s.push({type:"error",reason:a.stopReason,error:a}),s.end()}})(),s},F=(r,t,e)=>{const s=e?.apiKey||l(r.provider);if(!s)throw new Error(`No API key for provider: ${r.provider}`);const n=P(r,e,s),a=e?.reasoning?I(r,e.reasoning):void 0;return D(r,t,{...n,reasoningEffort:a==="off"?void 0:a})};function K(r){const t=r.trim().replace(/\/+$/,"");let e;try{e=new URL(t)}catch{throw new Error(`Invalid Azure OpenAI base URL: ${r}`)}const s=e.hostname.endsWith(".openai.azure.com")||e.hostname.endsWith(".cognitiveservices.azure.com"),n=e.pathname.replace(/\/+$/,"");return s&&(n===""||n==="/"||n==="/openai")&&(e.pathname="/openai/v1",e.search=""),e.toString().replace(/\/+$/,"")}function T(r){return`https://${r}.openai.azure.com/openai/v1`}function $(r,t){const e=t?.azureApiVersion||p.AZURE_OPENAI_API_VERSION||S,s=t?.azureBaseUrl?.trim()||p.AZURE_OPENAI_BASE_URL?.trim()||void 0,n=t?.azureResourceName||p.AZURE_OPENAI_RESOURCE_NAME;let a=s;if(!a&&n&&(a=T(n)),!a&&r.baseUrl&&(a=r.baseUrl),!a)throw new Error("Azure OpenAI base URL is required. Set AZURE_OPENAI_BASE_URL or AZURE_OPENAI_RESOURCE_NAME, or pass azureBaseUrl, azureResourceName, or model.baseUrl.");return{baseUrl:K(a),apiVersion:e}}function x(r,t,e){if(!t){if(!p.AZURE_OPENAI_API_KEY)throw new Error("Azure OpenAI API key is required. Set AZURE_OPENAI_API_KEY environment variable or pass it as an argument.");t=p.AZURE_OPENAI_API_KEY}const s={...r.headers};e?.headers&&Object.assign(s,e.headers);const{baseUrl:n,apiVersion:a}=$(r,e);return new U({apiKey:t,apiVersion:a,dangerouslyAllowBrowser:!0,defaultHeaders:s,baseURL:n})}function V(r,t,e,s){const n=N(r,t,M),a={model:s,input:n,stream:!0,prompt_cache_key:v(e?.sessionId)};if(e?.maxTokens&&(a.max_output_tokens=e?.maxTokens),e?.temperature!==void 0&&(a.temperature=e?.temperature),t.tools&&t.tools.length>0&&(a.tools=b(t.tools)),r.reasoning)if(e?.reasoningEffort||e?.reasoningSummary){const o=e?.reasoningEffort?r.thinkingLevelMap?.[e.reasoningEffort]??e.reasoningEffort:"medium";a.reasoning={effort:o,summary:e?.reasoningSummary||"auto"},a.include=["reasoning.encrypted_content"]}else r.thinkingLevelMap?.off!==null&&(a.reasoning={effort:r.thinkingLevelMap?.off??"none"});return a}export{D as streamAzureOpenAIResponses,F as streamSimpleAzureOpenAIResponses};
|
dist/assets/google-Bj49APZU.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
import{i as T,r as b,m as v,G as S,c as R,a as L,b as _}from"./google-shared-CZIKa0Kf.js";import{A as G,g as x,c as O,a as A}from"./index-BGwNxTBx.js";import{s as N}from"./sanitize-unicode-B62XMGgN.js";import{b as E}from"./transform-messages-Bxe2XUr4.js";let P=0;const f=(e,o,n)=>{const i=new G;return(async()=>{const t={role:"assistant",content:[],api:"google-generative-ai",provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const r=n?.apiKey||x(e.provider)||"",c=W(e,r,n?.headers);let u=B(e,o,n);const y=await n?.onPayload?.(u,e);y!==void 0&&(u=y);const M=await c.models.generateContentStream(u);i.push({type:"start",partial:t});let a=null;const C=t.content,l=()=>C.length-1;for await(const g of M){t.responseId||=g.responseId;const d=g.candidates?.[0];if(d?.content?.parts)for(const s of d.content.parts){if(s.text!==void 0){const h=T(s);(!a||h&&a.type!=="thinking"||!h&&a.type!=="text")&&(a&&(a.type==="text"?i.push({type:"text_end",contentIndex:C.length-1,content:a.text,partial:t}):i.push({type:"thinking_end",contentIndex:l(),content:a.thinking,partial:t})),h?(a={type:"thinking",thinking:"",thinkingSignature:void 0},t.content.push(a),i.push({type:"thinking_start",contentIndex:l(),partial:t})):(a={type:"text",text:""},t.content.push(a),i.push({type:"text_start",contentIndex:l(),partial:t}))),a.type==="thinking"?(a.thinking+=s.text,a.thinkingSignature=b(a.thinkingSignature,s.thoughtSignature),i.push({type:"thinking_delta",contentIndex:l(),delta:s.text,partial:t})):(a.text+=s.text,a.textSignature=b(a.textSignature,s.thoughtSignature),i.push({type:"text_delta",contentIndex:l(),delta:s.text,partial:t}))}if(s.functionCall){a&&(a.type==="text"?i.push({type:"text_end",contentIndex:l(),content:a.text,partial:t}):i.push({type:"thinking_end",contentIndex:l(),content:a.thinking,partial:t}),a=null);const h=s.functionCall.id,p={type:"toolCall",id:!h||t.content.some(I=>I.type==="toolCall"&&I.id===h)?`${s.functionCall.name}_${Date.now()}_${++P}`:h,name:s.functionCall.name||"",arguments:s.functionCall.args??{},...s.thoughtSignature&&{thoughtSignature:s.thoughtSignature}};t.content.push(p),i.push({type:"toolcall_start",contentIndex:l(),partial:t}),i.push({type:"toolcall_delta",contentIndex:l(),delta:JSON.stringify(p.arguments),partial:t}),i.push({type:"toolcall_end",contentIndex:l(),toolCall:p,partial:t})}}d?.finishReason&&(t.stopReason=v(d.finishReason),t.content.some(s=>s.type==="toolCall")&&(t.stopReason="toolUse")),g.usageMetadata&&(t.usage={input:(g.usageMetadata.promptTokenCount||0)-(g.usageMetadata.cachedContentTokenCount||0),output:(g.usageMetadata.candidatesTokenCount||0)+(g.usageMetadata.thoughtsTokenCount||0),cacheRead:g.usageMetadata.cachedContentTokenCount||0,cacheWrite:0,totalTokens:g.usageMetadata.totalTokenCount||0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},O(e,t.usage))}if(a&&(a.type==="text"?i.push({type:"text_end",contentIndex:l(),content:a.text,partial:t}):i.push({type:"thinking_end",contentIndex:l(),content:a.thinking,partial:t})),n?.signal?.aborted)throw new Error("Request was aborted");if(t.stopReason==="aborted"||t.stopReason==="error")throw new Error("An unknown error occurred");i.push({type:"done",reason:t.stopReason,message:t}),i.end()}catch(r){for(const c of t.content)"index"in c&&delete c.index;t.stopReason=n?.signal?.aborted?"aborted":"error",t.errorMessage=r instanceof Error?r.message:JSON.stringify(r),i.push({type:"error",reason:t.stopReason,error:t}),i.end()}})(),i},F=(e,o,n)=>{const i=n?.apiKey||x(e.provider);if(!i)throw new Error(`No API key for provider: ${e.provider}`);const t=E(e,n,i);if(!n?.reasoning)return f(e,o,{...t,thinking:{enabled:!1}});const r=A(e,n.reasoning),c=r==="off"?"high":r,u=e;return m(u)||w(u)||k(u)?f(e,o,{...t,thinking:{enabled:!0,level:K(c,u)}}):f(e,o,{...t,thinking:{enabled:!0,budgetTokens:U(u,c,n.thinkingBudgets)}})};function W(e,o,n){const i={};return e.baseUrl&&(i.baseUrl=e.baseUrl,i.apiVersion=""),(e.headers||n)&&(i.headers={...e.headers,...n}),new S({apiKey:o,httpOptions:Object.keys(i).length>0?i:void 0})}function B(e,o,n={}){const i=R(e,o),t={};n.temperature!==void 0&&(t.temperature=n.temperature),n.maxTokens!==void 0&&(t.maxOutputTokens=n.maxTokens);const r={...Object.keys(t).length>0&&t,...o.systemPrompt&&{systemInstruction:N(o.systemPrompt)},...o.tools&&o.tools.length>0&&{tools:L(o.tools)}};if(o.tools&&o.tools.length>0&&n.toolChoice?r.toolConfig={functionCallingConfig:{mode:_(n.toolChoice)}}:r.toolConfig=void 0,n.thinking?.enabled&&e.reasoning){const u={includeThoughts:!0};n.thinking.level!==void 0?u.thinkingLevel=n.thinking.level:n.thinking.budgetTokens!==void 0&&(u.thinkingBudget=n.thinking.budgetTokens),r.thinkingConfig=u}else e.reasoning&&n.thinking&&!n.thinking.enabled&&(r.thinkingConfig=H(e));if(n.signal){if(n.signal.aborted)throw new Error("Request aborted");r.abortSignal=n.signal}return{model:e.id,contents:i,config:r}}function k(e){return/gemma-?4/.test(e.id.toLowerCase())}function m(e){return/gemini-3(?:\.\d+)?-pro/.test(e.id.toLowerCase())}function w(e){return/gemini-3(?:\.\d+)?-flash/.test(e.id.toLowerCase())}function H(e){return m(e)?{thinkingLevel:"LOW"}:w(e)?{thinkingLevel:"MINIMAL"}:k(e)?{thinkingLevel:"MINIMAL"}:{thinkingBudget:0}}function K(e,o){if(m(o))switch(e){case"minimal":case"low":return"LOW";case"medium":case"high":return"HIGH"}if(k(o))switch(e){case"minimal":case"low":return"MINIMAL";case"medium":case"high":return"HIGH"}switch(e){case"minimal":return"MINIMAL";case"low":return"LOW";case"medium":return"MEDIUM";case"high":return"HIGH"}}function U(e,o,n){return n?.[o]!==void 0?n[o]:e.id.includes("2.5-pro")?{minimal:128,low:2048,medium:8192,high:32768}[o]:e.id.includes("2.5-flash-lite")?{minimal:512,low:2048,medium:8192,high:24576}[o]:e.id.includes("2.5-flash")?{minimal:128,low:2048,medium:8192,high:24576}[o]:-1}export{f as streamGoogle,F as streamSimpleGoogle};
|
dist/assets/google-vertex-BHrrHdVi.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
import{i as T,r as x,m as w,G as L,c as R,a as M,b as G,T as d,R as S}from"./google-shared-CZIKa0Kf.js";import{A,c as N,a as P}from"./index-BGwNxTBx.js";import{s as U}from"./sanitize-unicode-B62XMGgN.js";import{b as D}from"./transform-messages-Bxe2XUr4.js";var f={};const v="v1",V="gcp-vertex-credentials",H={THINKING_LEVEL_UNSPECIFIED:d.THINKING_LEVEL_UNSPECIFIED,MINIMAL:d.MINIMAL,LOW:d.LOW,MEDIUM:d.MEDIUM,HIGH:d.HIGH};let K=0;const k=(t,e,n)=>{const a=new A;return(async()=>{const i={role:"assistant",content:[],api:"google-vertex",provider:t.provider,model:t.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const s=$(n),l=s?j(t,s,n?.headers):W(t,F(n),z(n),n?.headers);let g=X(t,e,n);const I=await n?.onPayload?.(g,t);I!==void 0&&(g=I);const E=await l.models.generateContentStream(g);a.push({type:"start",partial:i});let o=null;const y=i.content,c=()=>y.length-1;for await(const u of E){i.responseId||=u.responseId;const p=u.candidates?.[0];if(p?.content?.parts)for(const r of p.content.parts){if(r.text!==void 0){const h=T(r);(!o||h&&o.type!=="thinking"||!h&&o.type!=="text")&&(o&&(o.type==="text"?a.push({type:"text_end",contentIndex:y.length-1,content:o.text,partial:i}):a.push({type:"thinking_end",contentIndex:c(),content:o.thinking,partial:i})),h?(o={type:"thinking",thinking:"",thinkingSignature:void 0},i.content.push(o),a.push({type:"thinking_start",contentIndex:c(),partial:i})):(o={type:"text",text:""},i.content.push(o),a.push({type:"text_start",contentIndex:c(),partial:i}))),o.type==="thinking"?(o.thinking+=r.text,o.thinkingSignature=x(o.thinkingSignature,r.thoughtSignature),a.push({type:"thinking_delta",contentIndex:c(),delta:r.text,partial:i})):(o.text+=r.text,o.textSignature=x(o.textSignature,r.thoughtSignature),a.push({type:"text_delta",contentIndex:c(),delta:r.text,partial:i}))}if(r.functionCall){o&&(o.type==="text"?a.push({type:"text_end",contentIndex:c(),content:o.text,partial:i}):a.push({type:"thinking_end",contentIndex:c(),content:o.thinking,partial:i}),o=null);const h=r.functionCall.id,m={type:"toolCall",id:!h||i.content.some(O=>O.type==="toolCall"&&O.id===h)?`${r.functionCall.name}_${Date.now()}_${++K}`:h,name:r.functionCall.name||"",arguments:r.functionCall.args??{},...r.thoughtSignature&&{thoughtSignature:r.thoughtSignature}};i.content.push(m),a.push({type:"toolcall_start",contentIndex:c(),partial:i}),a.push({type:"toolcall_delta",contentIndex:c(),delta:JSON.stringify(m.arguments),partial:i}),a.push({type:"toolcall_end",contentIndex:c(),toolCall:m,partial:i})}}p?.finishReason&&(i.stopReason=w(p.finishReason),i.content.some(r=>r.type==="toolCall")&&(i.stopReason="toolUse")),u.usageMetadata&&(i.usage={input:(u.usageMetadata.promptTokenCount||0)-(u.usageMetadata.cachedContentTokenCount||0),output:(u.usageMetadata.candidatesTokenCount||0)+(u.usageMetadata.thoughtsTokenCount||0),cacheRead:u.usageMetadata.cachedContentTokenCount||0,cacheWrite:0,totalTokens:u.usageMetadata.totalTokenCount||0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},N(t,i.usage))}if(o&&(o.type==="text"?a.push({type:"text_end",contentIndex:c(),content:o.text,partial:i}):a.push({type:"thinking_end",contentIndex:c(),content:o.thinking,partial:i})),n?.signal?.aborted)throw new Error("Request was aborted");if(i.stopReason==="aborted"||i.stopReason==="error")throw new Error("An unknown error occurred");a.push({type:"done",reason:i.stopReason,message:i}),a.end()}catch(s){for(const l of i.content)"index"in l&&delete l.index;i.stopReason=n?.signal?.aborted?"aborted":"error",i.errorMessage=s instanceof Error?s.message:JSON.stringify(s),a.push({type:"error",reason:i.stopReason,error:i}),a.end()}})(),a},rt=(t,e,n)=>{const a=D(t,n,void 0);if(!n?.reasoning)return k(t,e,{...a,thinking:{enabled:!1}});const i=P(t,n.reasoning),s=i==="off"?"high":i,l=t;return C(l)||b(l)?k(t,e,{...a,thinking:{enabled:!0,level:Q(s,l)}}):k(t,e,{...a,thinking:{enabled:!0,budgetTokens:Z(l,s,n.thinkingBudgets)}})};function W(t,e,n,a){return new L({vertexai:!0,project:e,location:n,apiVersion:v,httpOptions:_(t,a)})}function j(t,e,n){return new L({vertexai:!0,apiKey:e,apiVersion:v,httpOptions:_(t,n)})}function _(t,e){const n={},a=B(t.baseUrl);return a&&(n.baseUrl=a,n.baseUrlResourceScope=S.COLLECTION,J(a)&&(n.apiVersion="")),(t.headers||e)&&(n.headers={...t.headers,...e}),Object.keys(n).length>0?n:void 0}function B(t){const e=t.trim();if(!(!e||e.includes("{location}")))return e}function J(t){try{return new URL(t).pathname.split("/").some(n=>/^v\d+(?:beta\d*)?$/.test(n))}catch{return/(?:^|\/)v\d+(?:beta\d*)?(?:\/|$)/.test(t)}}function $(t){const e=t?.apiKey?.trim()||f.GOOGLE_CLOUD_API_KEY?.trim();if(!(!e||e===V||q(e)))return e}function q(t){return/^<[^>]+>$/.test(t)}function F(t){const e=t?.project||f.GOOGLE_CLOUD_PROJECT||f.GCLOUD_PROJECT;if(!e)throw new Error("Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT or pass project in options.");return e}function z(t){const e=t?.location||f.GOOGLE_CLOUD_LOCATION;if(!e)throw new Error("Vertex AI requires a location. Set GOOGLE_CLOUD_LOCATION or pass location in options.");return e}function X(t,e,n={}){const a=R(t,e),i={};n.temperature!==void 0&&(i.temperature=n.temperature),n.maxTokens!==void 0&&(i.maxOutputTokens=n.maxTokens);const s={...Object.keys(i).length>0&&i,...e.systemPrompt&&{systemInstruction:U(e.systemPrompt)},...e.tools&&e.tools.length>0&&{tools:M(e.tools)}};if(e.tools&&e.tools.length>0&&n.toolChoice?s.toolConfig={functionCallingConfig:{mode:G(n.toolChoice)}}:s.toolConfig=void 0,n.thinking?.enabled&&t.reasoning){const g={includeThoughts:!0};n.thinking.level!==void 0?g.thinkingLevel=H[n.thinking.level]:n.thinking.budgetTokens!==void 0&&(g.thinkingBudget=n.thinking.budgetTokens),s.thinkingConfig=g}else t.reasoning&&n.thinking&&!n.thinking.enabled&&(s.thinkingConfig=Y(t));if(n.signal){if(n.signal.aborted)throw new Error("Request aborted");s.abortSignal=n.signal}return{model:t.id,contents:a,config:s}}function C(t){return/gemini-3(?:\.\d+)?-pro/.test(t.id.toLowerCase())}function b(t){return/gemini-3(?:\.\d+)?-flash/.test(t.id.toLowerCase())}function Y(t){const e=t;return C(e)?{thinkingLevel:d.LOW}:b(e)?{thinkingLevel:d.MINIMAL}:{thinkingBudget:0}}function Q(t,e){if(C(e))switch(t){case"minimal":case"low":return"LOW";case"medium":case"high":return"HIGH"}switch(t){case"minimal":return"MINIMAL";case"low":return"LOW";case"medium":return"MEDIUM";case"high":return"HIGH"}}function Z(t,e,n){return n?.[e]!==void 0?n[e]:t.id.includes("2.5-pro")?{minimal:128,low:2048,medium:8192,high:32768}[e]:t.id.includes("2.5-flash")?{minimal:128,low:2048,medium:8192,high:24576}[e]:-1}export{k as streamGoogleVertex,rt as streamSimpleGoogleVertex};
|
dist/assets/index-BGwNxTBx.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dist/assets/json-parse-CyZFb4VN.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import{d as o}from"./index-BGwNxTBx.js";const a=new Set(['"',"\\","/","b","f","n","r","t","u"]);function f(t){const r=t.codePointAt(0);return r!==void 0&&r>=0&&r<=31}function d(t){switch(t){case"\b":return"\\b";case"\f":return"\\f";case`
|
| 2 |
+
`:return"\\n";case"\r":return"\\r";case" ":return"\\t";default:return`\\u${t.codePointAt(0)?.toString(16).padStart(4,"0")??"0000"}`}}function s(t){let r="",c=!1;for(let e=0;e<t.length;e++){const n=t[e];if(!c){r+=n,n==='"'&&(c=!0);continue}if(n==='"'){r+=n,c=!1;continue}if(n==="\\"){const i=t[e+1];if(i===void 0){r+="\\\\";continue}if(i==="u"){const u=t.slice(e+2,e+6);if(/^[0-9a-fA-F]{4}$/.test(u)){r+=`\\u${u}`,e+=5;continue}}if(a.has(i)){r+=`\\${i}`,e+=1;continue}r+="\\\\";continue}r+=f(n)?d(n):n}return r}function h(t){try{return JSON.parse(t)}catch(r){const c=s(t);if(c!==t)return JSON.parse(c);throw r}}function l(t){if(!t||t.trim()==="")return{};try{return h(t)}catch{try{return o.parse(t)??{}}catch{try{return o.parse(s(t))??{}}catch{return{}}}}}export{l as a,h as p};
|
dist/assets/mistral-BKzj2GJS.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
dist/assets/openai-codex-responses-C4HrSSHd.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import{A as ae,g as j,a as ie}from"./index-BGwNxTBx.js";import{h as X}from"./headers-CgnjaPPL.js";import{c as ce}from"./openai-prompt-cache-h_V5PL7P.js";import{c as V,a as ue,p as Y}from"./openai-responses-shared-CbKiZ_Nn.js";import{b as le}from"./transform-messages-Bxe2XUr4.js";import"./hash-DMDecQcg.js";import"./json-parse-CyZFb4VN.js";import"./sanitize-unicode-B62XMGgN.js";const $=new Set;function de(e){return $.add(e),()=>{$.delete(e)}}function A(e){return e instanceof Error?e.message||e.name:typeof e=="string"?e:String(e)}function fe(e){if(!(e instanceof Error))return{name:"ThrownValue",message:A(e)};const r=e.code;return{name:e.name||void 0,message:e.message||e.name,stack:e.stack,code:typeof r=="string"||typeof r=="number"?r:void 0}}function pe(e,r,t){return{type:e,timestamp:Date.now(),error:fe(r),details:t}}function ye(e,r){e.diagnostics=[...e.diagnostics??[],r]}var R={},me=function(e,r){return typeof e=="string"&&/^\.\.?\//.test(e)?e.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i,function(t,s,o,n,i){return s?".js":o&&(!n||!i)?t:o+n+"."+i.toLowerCase()+"js"}):e};let v=null;const z=e=>import(me(e)),be="node:os";typeof process<"u"&&(process.versions?.node||process.versions?.bun)&&z(be).then(e=>{v=e});const we="https://chatgpt.com/backend-api",he="https://api.openai.com/auth",W=3,F=1e3,G=new Set(["openai","openai-codex","opencode"]),ge=1009,Se=new Set(["completed","incomplete","failed","cancelled","queued","in_progress"]);function Ee(e,r){return e===429||e===500||e===502||e===503||e===504?!0:/rate.?limit|overloaded|service.?unavailable|upstream.?connect|connection.?refused/i.test(r)}function J(e,r){return new Promise((t,s)=>{if(r?.aborted){s(new Error("Request was aborted"));return}const o=setTimeout(t,e);r?.addEventListener("abort",()=>{clearTimeout(o),s(new Error("Request was aborted"))})})}const ve=(e,r,t)=>{const s=new ae;return(async()=>{const o={role:"assistant",content:[],api:"openai-codex-responses",provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const n=t?.apiKey||j(e.provider)||"";if(!n)throw new Error(`No API key for provider: ${e.provider}`);const i=Xe(n);let a=ke(e,r,t);const u=await t?.onPayload?.(a,e);u!==void 0&&(a=u);const l=t?.sessionId||Ve(),w=Ye(e.headers,t?.headers,i,n,t?.sessionId),g=ze(e.headers,t?.headers,i,n,l),d=JSON.stringify(a),b=t?.transport||"auto",f=b!=="sse"&&re(t?.sessionId);if(f&&U(t?.sessionId),b!=="sse"&&!f){let m=!1;try{if(await Ke(xe(e.baseUrl),a,g,o,s,e,()=>{m=!0},t),t?.signal?.aborted)throw new Error("Request was aborted");s.push({type:"done",reason:o.stopReason,message:o}),s.end();return}catch(p){if(t?.signal?.aborted||Te(p)||(ye(o,pe("provider_transport_failure",p,{configuredTransport:b,fallbackTransport:m?void 0:"sse",eventsEmitted:m,phase:m?"after_message_stream_start":"before_message_stream_start",requestBytes:new TextEncoder().encode(d).byteLength})),Le(t?.sessionId,p),m))throw p;U(t?.sessionId)}}let c,y;for(let m=0;m<=W;m++){if(t?.signal?.aborted)throw new Error("Request was aborted");try{if(c=await fetch(ee(e.baseUrl),{method:"POST",headers:w,body:d,signal:t?.signal}),await t?.onResponse?.({status:c.status,headers:X(c.headers)},e),c.ok)break;const p=await c.text();if(m<W&&Ee(c.status,p)){let x=F*2**m;const P=c.headers.get("retry-after-ms");if(P!==null){const E=Number(P);Number.isFinite(E)&&(x=Math.max(0,E))}else{const E=c.headers.get("retry-after");if(E){const B=Number(E);if(Number.isFinite(B))x=Math.max(0,B*1e3);else{const M=Date.parse(E);Number.isNaN(M)||(x=Math.max(0,M-Date.now()))}}}await J(x,t?.signal);continue}const _=new Response(p,{status:c.status,statusText:c.statusText}),N=await je(_);throw new Error(N.friendlyMessage||N.message)}catch(p){if(p instanceof Error&&(p.name==="AbortError"||p.message==="Request was aborted"))throw new Error("Request was aborted");if(y=p instanceof Error?p:new Error(String(p)),m<W&&!y.message.includes("usage limit")){const _=F*2**m;await J(_,t?.signal);continue}throw y}}if(!c?.ok)throw y??new Error("Failed after retries");if(!c.body)throw new Error("No response body");if(s.push({type:"start",partial:o}),await Re(c,o,s,e,t),t?.signal?.aborted)throw new Error("Request was aborted");s.push({type:"done",reason:o.stopReason,message:o}),s.end()}catch(n){for(const i of o.content)delete i.partialJson;o.stopReason=t?.signal?.aborted?"aborted":"error",o.errorMessage=n instanceof Error?n.message:String(n),s.push({type:"error",reason:o.stopReason,error:o}),s.end()}})(),s},nt=(e,r,t)=>{const s=t?.apiKey||j(e.provider);if(!s)throw new Error(`No API key for provider: ${e.provider}`);const o=le(e,t,s),n=t?.reasoning?ie(e,t.reasoning):void 0;return ve(e,r,{...o,reasoningEffort:n==="off"?void 0:n})};function ke(e,r,t){const s=V(e,r,G,{includeSystemPrompt:!1}),o={model:e.id,store:!1,stream:!0,instructions:r.systemPrompt||"You are a helpful assistant.",input:s,text:{verbosity:t?.textVerbosity||"low"},include:["reasoning.encrypted_content"],prompt_cache_key:ce(t?.sessionId),tool_choice:"auto",parallel_tool_calls:!0};if(t?.temperature!==void 0&&(o.temperature=t.temperature),t?.serviceTier!==void 0&&(o.service_tier=t.serviceTier),r.tools&&r.tools.length>0&&(o.tools=ue(r.tools,{strict:null})),t?.reasoningEffort!==void 0){const n=t.reasoningEffort==="none"?e.thinkingLevelMap?.off??"none":e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort;n!==null&&(o.reasoning={effort:n,summary:t.reasoningSummary??"auto"})}return o}function _e(e,r){switch(r){case"flex":return .5;case"priority":return e.id==="gpt-5.5"?2.5:2;default:return 1}}function Q(e,r,t){const s=_e(t,r);s!==1&&(e.cost.input*=s,e.cost.output*=s,e.cost.cacheRead*=s,e.cost.cacheWrite*=s,e.cost.total=e.cost.input+e.cost.output+e.cost.cacheRead+e.cost.cacheWrite)}function Z(e,r){return e==="default"&&(r==="flex"||r==="priority")?r:e??r}function ee(e){const t=(e&&e.trim().length>0?e:we).replace(/\/+$/,"");return t.endsWith("/codex/responses")?t:t.endsWith("/codex")?`${t}/responses`:`${t}/codex/responses`}function xe(e){const r=new URL(ee(e));return r.protocol==="https:"&&(r.protocol="wss:"),r.protocol==="http:"&&(r.protocol="ws:"),r.toString()}async function Re(e,r,t,s,o){await Y(te(Oe(e)),r,t,s,{serviceTier:o?.serviceTier,resolveServiceTier:Z,applyServiceTierPricing:(n,i)=>Q(n,i,s)})}class L extends Error{code;payload;constructor(r,t){super(r),this.name="CodexApiError",this.code=t?.code,this.payload=t?.payload,this.cause=t?.cause}}class q extends Error{payload;constructor(r,t){super(r),this.name="CodexProtocolError",this.payload=t?.payload,this.cause=t?.cause}}function Te(e){return e instanceof L||e instanceof q}async function*te(e){for await(const r of e){const t=typeof r.type=="string"?r.type:void 0;if(t){if(t==="error"){const s=r.code||"",o=r.message||"";throw new L(`Codex error: ${o||s||JSON.stringify(r)}`,{code:s||void 0,payload:r})}if(t==="response.failed"){const s=r.response,o=s?.error?.code,n=s?.error?.message;throw new L(n||"Codex response failed",{code:o,payload:r})}if(t==="response.done"||t==="response.completed"||t==="response.incomplete"){const s=r.response,o=s&&{...s,status:Ce(s.status)};yield{...r,type:"response.completed",response:o};return}yield r}}}function Ce(e){if(typeof e=="string")return Se.has(e)?e:void 0}async function*Oe(e){if(!e.body)return;const r=e.body.getReader(),t=new TextDecoder;let s="";try{for(;;){const{done:o,value:n}=await r.read();if(o)break;s+=t.decode(n,{stream:!0});let i=s.indexOf(`
|
| 2 |
+
|
| 3 |
+
`);for(;i!==-1;){const a=s.slice(0,i);s=s.slice(i+2);const u=a.split(`
|
| 4 |
+
`).filter(l=>l.startsWith("data:")).map(l=>l.slice(5).trim());if(u.length>0){const l=u.join(`
|
| 5 |
+
`).trim();if(l&&l!=="[DONE]")try{yield JSON.parse(l)}catch(w){throw new q(`Invalid Codex SSE JSON: ${A(w)}`,{cause:w,payload:l})}}i=s.indexOf(`
|
| 6 |
+
|
| 7 |
+
`)}}}finally{try{await r.cancel()}catch{}try{r.releaseLock()}catch{}}}const Ae="responses_websockets=2026-02-06",We=300*1e3,h=new Map,k=new Map,O=new Set;function D(e){let r=k.get(e);return r||(r={requests:0,connectionsCreated:0,connectionsReused:0,cachedContextRequests:0,storeTrueRequests:0,fullContextRequests:0,deltaRequests:0,lastInputItems:0,websocketFailures:0,sseFallbacks:0},k.set(e,r)),r}function at(e){const r=k.get(e);return r?{...r}:void 0}function it(e){if(e){k.delete(e),O.delete(e);return}k.clear(),O.clear()}function Ie(e){const r=t=>{t.idleTimer&&clearTimeout(t.idleTimer),S(t.socket,1e3,"debug_close")};if(e){const t=h.get(e);t&&r(t),h.delete(e);return}for(const t of h.values())r(t);h.clear()}de(Ie);function re(e){return e?O.has(e):!1}function U(e){if(!e)return;const r=D(e);r.sseFallbacks++,r.websocketFallbackActive=re(e)}function Le(e,r){if(!e)return;O.add(e);const t=D(e);t.websocketFailures++,t.lastWebSocketError=A(r),t.websocketFallbackActive=!0}let T=null;async function qe(){if(T)return T;if(process?.versions?.bun&&(R.HTTP_PROXY||R.HTTPS_PROXY||R.http_proxy||R.https_proxy)){const t=(await z("proxy-from-env")).getProxyForUrl;return T=class extends WebSocket{constructor(s,o){let n={};Array.isArray(o)||typeof o=="string"?n={protocols:o}:n={...o};const i=t(s.toString().replace(/^wss:/,"https:").replace(/^ws:/,"http:"));super(s,{...n,...i?{proxy:i}:{}})}},T}const e=globalThis.WebSocket;return typeof e!="function"?null:e}class De extends Error{code;reason;wasClean;constructor(r,t){super(r),this.name="WebSocketCloseError",this.code=t?.code,this.reason=t?.reason,this.wasClean=t?.wasClean}}function Ne(e){const r=e.readyState;return typeof r=="number"?r:void 0}function C(e){const r=Ne(e);return r===void 0||r===1}function S(e,r=1e3,t="done"){try{e.close(r,t)}catch{}}function H(e,r){r.idleTimer&&clearTimeout(r.idleTimer),r.idleTimer=setTimeout(()=>{r.busy||(S(r.socket,1e3,"idle_timeout"),h.delete(e))},We)}async function I(e,r,t){const s=await qe();if(!s)throw new Error("WebSocket transport is not available in this runtime");const o=X(r);return delete o["OpenAI-Beta"],new Promise((n,i)=>{let a=!1,u;try{u=new s(e,{headers:o})}catch(f){i(f instanceof Error?f:new Error(String(f)));return}const l=()=>{a||(a=!0,b(),n(u))},w=f=>{const c=se(f);a||(a=!0,b(),i(c))},g=f=>{const c=oe(f);a||(a=!0,b(),i(c))},d=()=>{a||(a=!0,b(),u.close(1e3,"aborted"),i(new Error("Request was aborted")))},b=()=>{u.removeEventListener("open",l),u.removeEventListener("error",w),u.removeEventListener("close",g),t?.removeEventListener("abort",d)};u.addEventListener("open",l),u.addEventListener("error",w),u.addEventListener("close",g),t?.addEventListener("abort",d)})}async function Pe(e,r,t,s){if(!t){const a=await I(e,r,s);return{socket:a,reused:!1,release:({keep:u}={})=>{if(u===!1){S(a);return}S(a)}}}const o=h.get(t);if(o){if(o.idleTimer&&(clearTimeout(o.idleTimer),o.idleTimer=void 0),!o.busy&&C(o.socket))return o.busy=!0,{socket:o.socket,entry:o,reused:!0,release:({keep:a}={})=>{if(!a||!C(o.socket)){S(o.socket),h.delete(t);return}o.busy=!1,H(t,o)}};if(o.busy){const a=await I(e,r,s);return{socket:a,reused:!1,release:()=>{S(a)}}}C(o.socket)||(S(o.socket),h.delete(t))}const n=await I(e,r,s),i={socket:n,busy:!0};return h.set(t,i),{socket:n,entry:i,reused:!1,release:({keep:a}={})=>{if(!a||!C(i.socket)){S(i.socket),i.idleTimer&&clearTimeout(i.idleTimer),h.get(t)===i&&h.delete(t);return}i.busy=!1,H(t,i)}}}function se(e){if(e&&typeof e=="object"){const r="message"in e?e.message:void 0;if(typeof r=="string"&&r.length>0)return new Error(r);const t="error"in e?e.error:void 0;if(t instanceof Error&&t.message.length>0)return t;if(t&&typeof t=="object"&&"message"in t){const s=t.message;if(typeof s=="string"&&s.length>0)return new Error(s)}}return new Error("WebSocket error")}function oe(e){if(e&&typeof e=="object"){const r="code"in e?e.code:void 0,t="reason"in e?e.reason:void 0,s="wasClean"in e?e.wasClean:void 0,o=typeof r=="number"?` ${r}`:"";let n=typeof t=="string"&&t.length>0?` ${t}`:"";return!n&&r===ge&&(n=" message too big"),new De(`WebSocket closed${o}${n}`.trim(),{code:typeof r=="number"?r:void 0,reason:typeof t=="string"&&t.length>0?t:void 0,wasClean:typeof s=="boolean"?s:void 0})}return new Error("WebSocket closed")}async function Be(e){if(typeof e=="string")return e;if(e instanceof ArrayBuffer)return new TextDecoder().decode(new Uint8Array(e));if(ArrayBuffer.isView(e)){const r=e;return new TextDecoder().decode(new Uint8Array(r.buffer,r.byteOffset,r.byteLength))}if(e&&typeof e=="object"&&"arrayBuffer"in e){const t=await e.arrayBuffer();return new TextDecoder().decode(new Uint8Array(t))}return null}async function*Me(e,r){const t=[];let s=null,o=!1,n=null,i=!1;const a=()=>{if(!s)return;const d=s;s=null,d()},u=d=>{(async()=>{let b=null;try{if(!d||typeof d!="object"||!("data"in d)||(b=await Be(d.data),!b))return;const f=JSON.parse(b),c=typeof f.type=="string"?f.type:"";(c==="response.completed"||c==="response.done"||c==="response.incomplete")&&(i=!0,o=!0),t.push(f),a()}catch(f){n=new q(`Invalid Codex WebSocket JSON: ${A(f)}`,{cause:f,payload:b}),o=!0,a()}})()},l=d=>{n=se(d),o=!0,a()},w=d=>{if(i){o=!0,a();return}n||(n=oe(d)),o=!0,a()},g=()=>{n=new Error("Request was aborted"),o=!0,a()};e.addEventListener("message",u),e.addEventListener("error",l),e.addEventListener("close",w),r?.addEventListener("abort",g);try{for(;;){if(r?.aborted)throw new Error("Request was aborted");if(t.length>0){yield t.shift();continue}if(o)break;await new Promise(d=>{s=d})}if(n)throw n;if(!i)throw new Error("WebSocket stream closed before response.completed")}finally{e.removeEventListener("message",u),e.removeEventListener("error",l),e.removeEventListener("close",w),r?.removeEventListener("abort",g)}}function K(e){const{input:r,previous_response_id:t,...s}=e;return s}function $e(e,r){return JSON.stringify(e??[])===JSON.stringify(r??[])}function Fe(e,r){return JSON.stringify(K(e))===JSON.stringify(K(r))}function Je(e,r){if(!Fe(e,r.lastRequestBody))return;const t=e.input??[],s=[...r.lastRequestBody.input??[],...r.lastResponseItems];if(t.length<s.length)return;const o=t.slice(0,s.length);if($e(o,s))return t.slice(s.length)}function Ue(e,r){const t=e.continuation;if(!t)return r;const s=Je(r,t);return!s||!t.lastResponseId?(e.continuation=void 0,r):{...r,previous_response_id:t.lastResponseId,input:s}}async function*He(e,r,t,s){let o=!1;for await(const n of e)o||(o=!0,s(),t.push({type:"start",partial:r})),yield n}async function Ke(e,r,t,s,o,n,i,a){const{socket:u,entry:l,reused:w,release:g}=await Pe(e,t,a?.sessionId,a?.signal);let d=!0;const b=a?.transport==="websocket-cached"||a?.transport==="auto",f=r,c=b&&l?Ue(l,f):f,y=a?.sessionId?D(a.sessionId):void 0;y&&(y.requests++,w?y.connectionsReused++:y.connectionsCreated++,b&&y.cachedContextRequests++,c.store===!0&&y.storeTrueRequests++,y.lastInputItems=c.input?.length??0,c.previous_response_id?(y.deltaRequests++,y.lastDeltaInputItems=c.input?.length??0,y.lastPreviousResponseId=c.previous_response_id):(y.fullContextRequests++,y.lastDeltaInputItems=void 0,y.lastPreviousResponseId=void 0));try{if(u.send(JSON.stringify({type:"response.create",...c})),await Y(He(te(Me(u,a?.signal)),s,o,i),s,o,n,{serviceTier:a?.serviceTier,resolveServiceTier:Z,applyServiceTierPricing:(m,p)=>Q(m,p,n)}),a?.signal?.aborted)d=!1;else if(b&&l&&s.responseId){const m=V(n,{messages:[s]},G,{includeSystemPrompt:!1}).filter(p=>p.type!=="function_call_output");l.continuation={lastRequestBody:f,lastResponseId:s.responseId,lastResponseItems:m}}}catch(m){throw l&&(l.continuation=void 0),d=!1,m}finally{g({keep:d})}}async function je(e){const r=await e.text();let t=r||e.statusText||"Request failed",s;try{const n=JSON.parse(r)?.error;if(n){const i=n.code||n.type||"";if(/usage_limit_reached|usage_not_included|rate_limit_exceeded/i.test(i)||e.status===429){const a=n.plan_type?` (${n.plan_type.toLowerCase()} plan)`:"",u=n.resets_at?Math.max(0,Math.round((n.resets_at*1e3-Date.now())/6e4)):void 0,l=u!==void 0?` Try again in ~${u} min.`:"";s=`You have hit your ChatGPT usage limit${a}.${l}`.trim()}t=n.message||s||t}}catch{}return{message:t,friendlyMessage:s}}function Xe(e){try{const r=e.split(".");if(r.length!==3)throw new Error("Invalid token");const s=JSON.parse(atob(r[1]))?.[he]?.chatgpt_account_id;if(!s)throw new Error("No account ID in token");return s}catch{throw new Error("Failed to extract accountId from token")}}function Ve(){return typeof globalThis.crypto?.randomUUID=="function"?globalThis.crypto.randomUUID():`codex_${Date.now()}_${Math.random().toString(36).slice(2,10)}`}function ne(e,r,t,s){const o=new Headers(e);for(const[i,a]of Object.entries(r||{}))o.set(i,a);o.set("Authorization",`Bearer ${s}`),o.set("chatgpt-account-id",t),o.set("originator","pi");const n=v?`pi (${v.platform()} ${v.release()}; ${v.arch()})`:"pi (browser)";return o.set("User-Agent",n),o}function Ye(e,r,t,s,o){const n=ne(e,r,t,s);return n.set("OpenAI-Beta","responses=experimental"),n.set("accept","text/event-stream"),n.set("content-type","application/json"),o&&(n.set("session_id",o),n.set("x-client-request-id",o)),n}function ze(e,r,t,s,o){const n=ne(e,r,t,s);return n.delete("accept"),n.delete("content-type"),n.delete("OpenAI-Beta"),n.delete("openai-beta"),n.set("OpenAI-Beta",Ae),n.set("x-client-request-id",o),n.set("session_id",o),n}export{Ie as closeOpenAICodexWebSocketSessions,at as getOpenAICodexWebSocketDebugStats,it as resetOpenAICodexWebSocketDebugStats,ve as streamOpenAICodexResponses,nt as streamSimpleOpenAICodexResponses};
|
dist/assets/openai-completions-ClwNjci9.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-BGwNxTBx.js";import{h as J}from"./headers-CgnjaPPL.js";import{a as P}from"./json-parse-CyZFb4VN.js";import{s as C}from"./sanitize-unicode-B62XMGgN.js";import{h as Y,b as V,i as Z,r as Q}from"./github-copilot-headers-CVWOSrQr.js";import{c as X}from"./openai-prompt-cache-h_V5PL7P.js";import{t as ee,b as te}from"./transform-messages-Bxe2XUr4.js";var b={};function ne(e){for(const n of e)if(n.role==="toolResult"||n.role==="assistant"&&n.content.some(t=>t.type==="toolCall"))return!0;return!1}function F(e){return e.type==="text"}function se(e){return e.type==="thinking"}function oe(e){return e.type==="toolCall"}function re(e){return e.type==="image"}function N(e){return e||(typeof process<"u"&&b.PI_CACHE_RETENTION==="long"?"long":"short")}const ae=(e,n,t)=>{const s=new $;return(async()=>{const r={role:"assistant",content:[],api:e.api,provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const c=t?.apiKey||L(e.provider)||"",u=q(e),o=N(t?.cacheRetention),i=o==="none"?void 0:t?.sessionId,p=ie(e,n,c,t?.headers,i,u);let h=le(e,n,t,u,o);const y=await t?.onPayload?.(h,e);y!==void 0&&(h=y);const R={...t?.signal?{signal:t.signal}:{},...t?.timeoutMs!==void 0?{timeout:t.timeoutMs}:{},...t?.maxRetries!==void 0?{maxRetries:t.maxRetries}:{}},{data:T,response:_}=await p.chat.completions.create(h,R).withResponse();await t?.onResponse?.({status:_.status,headers:J(_.headers)},e),s.push({type:"start",partial:r});let v=null,l=null,k=!1;const E=new Map,S=new Map,I=r.content,A=a=>I.indexOf(a),D=a=>{const f=A(a);f!==-1&&(a.type==="text"?s.push({type:"text_end",contentIndex:f,content:a.text,partial:r}):a.type==="thinking"?s.push({type:"thinking_end",contentIndex:f,content:a.thinking,partial:r}):a.type==="toolCall"&&(a.arguments=P(a.partialArgs),delete a.partialArgs,delete a.streamIndex,s.push({type:"toolcall_end",contentIndex:f,toolCall:a,partial:r})))},U=()=>(v||(v={type:"text",text:""},I.push(v),s.push({type:"text_start",contentIndex:A(v),partial:r})),v),H=a=>(l||(l={type:"thinking",thinking:"",thinkingSignature:a},I.push(l),s.push({type:"thinking_start",contentIndex:A(l),partial:r})),l),j=a=>{const f=typeof a.index=="number"?a.index:void 0;let d=f!==void 0?E.get(f):void 0;return!d&&a.id&&(d=S.get(a.id)),d||(d={type:"toolCall",id:a.id||"",name:a.function?.name||"",arguments:{},partialArgs:"",streamIndex:f},f!==void 0&&E.set(f,d),a.id&&S.set(a.id,d),I.push(d),s.push({type:"toolcall_start",contentIndex:A(d),partial:r})),f!==void 0&&d.streamIndex===void 0&&(d.streamIndex=f,E.set(f,d)),a.id&&S.set(a.id,d),d};for await(const a of T){if(!a||typeof a!="object")continue;r.responseId||=a.id,typeof a.model=="string"&&a.model.length>0&&a.model!==e.id&&(r.responseModel||=a.model),a.usage&&(r.usage=B(a.usage,e));const f=Array.isArray(a.choices)?a.choices[0]:void 0;if(f){if(!a.usage&&f.usage&&(r.usage=B(f.usage,e)),f.finish_reason){const d=ke(f.finish_reason);r.stopReason=d.stopReason,d.errorMessage&&(r.errorMessage=d.errorMessage),k=!0}if(f.delta){if(f.delta.content!==null&&f.delta.content!==void 0&&f.delta.content.length>0){const g=U();g.text+=f.delta.content,s.push({type:"text_delta",contentIndex:A(g),delta:f.delta.content,partial:r})}const d=["reasoning_content","reasoning","reasoning_text"],O=f.delta;let w=null;for(const g of d){const m=O[g];if(typeof m=="string"&&m.length>0){w=g;break}}if(w){const g=O[w];if(typeof g=="string"&&g.length>0){const m=e.provider==="opencode-go"&&w==="reasoning"?"reasoning_content":w,x=H(m);x.thinking+=g,s.push({type:"thinking_delta",contentIndex:A(x),delta:g,partial:r})}}if(f?.delta?.tool_calls)for(const g of f.delta.tool_calls){const m=j(g);!m.id&&g.id&&(m.id=g.id,S.set(g.id,m)),!m.name&&g.function?.name&&(m.name=g.function.name);let x="";g.function?.arguments&&(x=g.function.arguments,m.partialArgs=(m.partialArgs??"")+g.function.arguments,m.arguments=P(m.partialArgs)),s.push({type:"toolcall_delta",contentIndex:A(m),delta:x,partial:r})}const M=f.delta.reasoning_details;if(M&&Array.isArray(M)){for(const g of M)if(g.type==="reasoning.encrypted"&&g.id&&g.data){const m=r.content.find(x=>x.type==="toolCall"&&x.id===g.id);m&&(m.thoughtSignature=JSON.stringify(g))}}}}}for(const a of I)D(a);if(t?.signal?.aborted)throw new Error("Request was aborted");if(r.stopReason==="aborted")throw new Error("Request was aborted");if(r.stopReason==="error")throw new Error(r.errorMessage||"Provider returned an error stop reason");if(!k)throw new Error("Stream ended without finish_reason");s.push({type:"done",reason:r.stopReason,message:r}),s.end()}catch(c){for(const o of r.content)delete o.index,delete o.partialArgs,delete o.streamIndex;r.stopReason=t?.signal?.aborted?"aborted":"error",r.errorMessage=c instanceof Error?c.message:JSON.stringify(c);const u=c?.error?.metadata?.raw;u&&(r.errorMessage+=`
|
| 2 |
+
${u}`),s.push({type:"error",reason:r.stopReason,error:r}),s.end()}})(),s},Se=(e,n,t)=>{const s=t?.apiKey||L(e.provider);if(!s)throw new Error(`No API key for provider: ${e.provider}`);const r=te(e,t,s),c=t?.reasoning?W(e,t.reasoning):void 0,u=c==="off"?void 0:c,o=t?.toolChoice;return ae(e,n,{...r,reasoningEffort:u,toolChoice:o})};function ie(e,n,t,s,r,c=q(e)){if(!t){if(!b.OPENAI_API_KEY)throw new Error("OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.");t=b.OPENAI_API_KEY}const u={...e.headers};if(e.provider==="github-copilot"){const i=Y(n.messages),p=V({messages:n.messages,hasImages:i});Object.assign(u,p)}r&&c.sendSessionAffinityHeaders&&(u.session_id=r,u["x-client-request-id"]=r,u["x-session-affinity"]=r),s&&Object.assign(u,s);const o=e.provider==="cloudflare-ai-gateway"?{...u,Authorization:u.Authorization??null,"cf-aig-authorization":`Bearer ${t}`}:u;return new G({apiKey:t,baseURL:Z(e.provider)?Q(e):e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:o})}function le(e,n,t,s=q(e),r=N(t?.cacheRetention)){const c=me(e,n,s),u=ce(s,r),o={model:e.id,messages:c,stream:!0,prompt_cache_key:e.baseUrl.includes("api.openai.com")&&r!=="none"||r==="long"&&s.supportsLongCacheRetention?X(t?.sessionId):void 0,prompt_cache_retention:r==="long"&&s.supportsLongCacheRetention?"24h":void 0};if(s.supportsUsageInStreaming!==!1&&(o.stream_options={include_usage:!0}),s.supportsStore&&(o.store=!1),t?.maxTokens&&(s.maxTokensField==="max_tokens"?o.max_tokens=t.maxTokens:o.max_completion_tokens=t.maxTokens),t?.temperature!==void 0&&(o.temperature=t.temperature),n.tools&&n.tools.length>0?(o.tools=ye(n.tools,s),s.zaiToolStream&&(o.tool_stream=!0)):ne(n.messages)&&(o.tools=[]),u&&ue(c,o.tools,u),t?.toolChoice&&(o.tool_choice=t.toolChoice),s.thinkingFormat==="zai"&&e.reasoning)o.enable_thinking=!!t?.reasoningEffort;else if(s.thinkingFormat==="qwen"&&e.reasoning)o.enable_thinking=!!t?.reasoningEffort;else if(s.thinkingFormat==="qwen-chat-template"&&e.reasoning)o.chat_template_kwargs={enable_thinking:!!t?.reasoningEffort,preserve_thinking:!0};else if(s.thinkingFormat==="deepseek"&&e.reasoning)o.thinking={type:t?.reasoningEffort?"enabled":"disabled"},t?.reasoningEffort&&(o.reasoning_effort=e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort);else if(s.thinkingFormat==="openrouter"&&e.reasoning){const i=o;t?.reasoningEffort?i.reasoning={effort:e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort}:e.thinkingLevelMap?.off!==null&&(i.reasoning={effort:e.thinkingLevelMap?.off??"none"})}else if(s.thinkingFormat==="together"&&e.reasoning){const i=o;i.reasoning={enabled:!!t?.reasoningEffort},t?.reasoningEffort&&s.supportsReasoningEffort&&(i.reasoning_effort=e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort)}else if(t?.reasoningEffort&&e.reasoning&&s.supportsReasoningEffort)o.reasoning_effort=e.thinkingLevelMap?.[t.reasoningEffort]??t.reasoningEffort;else if(!t?.reasoningEffort&&e.reasoning&&s.supportsReasoningEffort){const i=e.thinkingLevelMap?.off;typeof i=="string"&&(o.reasoning_effort=i)}if(e.baseUrl.includes("openrouter.ai")&&e.compat?.openRouterRouting&&(o.provider=e.compat.openRouterRouting),e.baseUrl.includes("ai-gateway.vercel.sh")&&e.compat?.vercelGatewayRouting){const i=e.compat.vercelGatewayRouting;if(i.only||i.order){const p={};i.only&&(p.only=i.only),i.order&&(p.order=i.order),o.providerOptions={gateway:p}}}return o}function ce(e,n){if(e.cacheControlFormat!=="anthropic"||n==="none")return;const t=n==="long"&&e.supportsLongCacheRetention?"1h":void 0;return{type:"ephemeral",...t?{ttl:t}:{}}}function ue(e,n,t){fe(e,t),ge(n,t),pe(e,t)}function fe(e,n){for(const t of e)if(t.role==="system"||t.role==="developer"){he(t,n);return}}function pe(e,n){for(let t=e.length-1;t>=0;t--){const s=e[t];if((s.role==="user"||s.role==="assistant")&&de(s,n))return}}function ge(e,n){if(!e||e.length===0)return;const t=e[e.length-1];t.cache_control=n}function he(e,n){return z(e,n)}function de(e,n){return e.role==="user"||e.role==="assistant"?z(e,n):!1}function z(e,n){const t=e.content;if(typeof t=="string")return t.length===0?!1:(e.content=[{type:"text",text:t,cache_control:n}],!0);if(!Array.isArray(t))return!1;for(let s=t.length-1;s>=0;s--){const r=t[s];if(r?.type==="text"){const c=r;return c.cache_control=n,!0}}return!1}function me(e,n,t){const s=[],r=o=>{if(o.includes("|")){const[i]=o.split("|");return i.replace(/[^a-zA-Z0-9_-]/g,"_").slice(0,40)}return e.provider==="openai"&&o.length>40?o.slice(0,40):o},c=ee(n.messages,e,o=>r(o));if(n.systemPrompt){const i=e.reasoning&&t.supportsDeveloperRole?"developer":"system";s.push({role:i,content:C(n.systemPrompt)})}let u=null;for(let o=0;o<c.length;o++){const i=c[o];if(t.requiresAssistantAfterToolResult&&u==="toolResult"&&i.role==="user"&&s.push({role:"assistant",content:"I have processed the tool results."}),i.role==="user")if(typeof i.content=="string")s.push({role:"user",content:C(i.content)});else{const p=i.content.map(h=>h.type==="text"?{type:"text",text:C(h.text)}:{type:"image_url",image_url:{url:`data:${h.mimeType};base64,${h.data}`}});if(p.length===0)continue;s.push({role:"user",content:p})}else if(i.role==="assistant"){const p={role:"assistant",content:t.requiresAssistantAfterToolResult?"":null},h=i.content.filter(F).filter(l=>l.text.trim().length>0).map(l=>({type:"text",text:C(l.text)})),y=h.map(l=>l.text).join(""),R=i.content.filter(se).filter(l=>l.thinking.trim().length>0);if(R.length>0)if(t.requiresThinkingAsText){const l=R.map(k=>C(k.thinking)).join(`
|
| 3 |
+
|
| 4 |
+
`);p.content=[{type:"text",text:l},...h]}else{y.length>0&&(p.content=y);let l=R[0].thinkingSignature;e.provider==="opencode-go"&&l==="reasoning"&&(l="reasoning_content"),l&&l.length>0&&(p[l]=R.map(k=>k.thinking).join(`
|
| 5 |
+
`))}else y.length>0&&(p.content=y);const T=i.content.filter(oe);if(T.length>0){p.tool_calls=T.map(k=>({id:k.id,type:"function",function:{name:k.name,arguments:JSON.stringify(k.arguments)}}));const l=T.filter(k=>k.thoughtSignature).map(k=>{try{return JSON.parse(k.thoughtSignature)}catch{return null}}).filter(Boolean);l.length>0&&(p.reasoning_details=l)}t.requiresReasoningContentOnAssistantMessages&&e.reasoning&&p.reasoning_content===void 0&&(p.reasoning_content="");const _=p.content;if(!(_!=null&&_.length>0)&&!p.tool_calls)continue;s.push(p)}else if(i.role==="toolResult"){const p=[];let h=o;for(;h<c.length&&c[h].role==="toolResult";h++){const y=c[h],R=y.content.filter(F).map(l=>l.text).join(`
|
| 6 |
+
`),T=y.content.some(l=>l.type==="image"),_=R.length>0,v={role:"tool",content:C(_?R:"(see attached image)"),tool_call_id:y.toolCallId};if(t.requiresToolResultName&&y.toolName&&(v.name=y.toolName),s.push(v),T&&e.input.includes("image"))for(const l of y.content)re(l)&&p.push({type:"image_url",image_url:{url:`data:${l.mimeType};base64,${l.data}`}})}o=h-1,p.length>0?(t.requiresAssistantAfterToolResult&&s.push({role:"assistant",content:"I have processed the tool results."}),s.push({role:"user",content:[{type:"text",text:"Attached image(s) from tool result:"},...p]}),u="user"):u="toolResult";continue}u=i.role}return s}function ye(e,n){return e.map(t=>({type:"function",function:{name:t.name,description:t.description,parameters:t.parameters,...n.supportsStrictMode!==!1&&{strict:!1}}}))}function B(e,n){const t=e.prompt_tokens||0,s=e.prompt_tokens_details?.cached_tokens??e.prompt_cache_hit_tokens??0,r=e.prompt_tokens_details?.cache_write_tokens||0,c=Math.max(0,t-s-r),u=e.completion_tokens||0,o={input:c,output:u,cacheRead:s,cacheWrite:r,totalTokens:c+u+s+r,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}};return K(n,o),o}function ke(e){if(e===null)return{stopReason:"stop"};switch(e){case"stop":case"end":return{stopReason:"stop"};case"length":return{stopReason:"length"};case"function_call":case"tool_calls":return{stopReason:"toolUse"};case"content_filter":return{stopReason:"error",errorMessage:"Provider finish_reason: content_filter"};case"network_error":return{stopReason:"error",errorMessage:"Provider finish_reason: network_error"};default:return{stopReason:"error",errorMessage:`Provider finish_reason: ${e}`}}}function Re(e){const n=e.provider,t=e.baseUrl,s=n==="zai"||t.includes("api.z.ai"),r=n==="together"||t.includes("api.together.ai")||t.includes("api.together.xyz"),c=n==="moonshotai"||n==="moonshotai-cn"||t.includes("api.moonshot."),u=n==="cloudflare-workers-ai"||t.includes("api.cloudflare.com"),o=n==="cloudflare-ai-gateway"||t.includes("gateway.ai.cloudflare.com"),i=n==="cerebras"||t.includes("cerebras.ai")||n==="xai"||t.includes("api.x.ai")||r||t.includes("chutes.ai")||t.includes("deepseek.com")||s||c||n==="opencode"||t.includes("opencode.ai")||u||o,p=t.includes("chutes.ai")||c||o||r,h=n==="xai"||t.includes("api.x.ai"),y=n==="deepseek"||t.includes("deepseek.com"),R=n==="openrouter"&&e.id.startsWith("anthropic/")?"anthropic":void 0;return{supportsStore:!i,supportsDeveloperRole:!i,supportsReasoningEffort:!h&&!s&&!c&&!r&&!o,supportsUsageInStreaming:!0,maxTokensField:p?"max_tokens":"max_completion_tokens",requiresToolResultName:!1,requiresAssistantAfterToolResult:!1,requiresThinkingAsText:!1,requiresReasoningContentOnAssistantMessages:y,thinkingFormat:y?"deepseek":s?"zai":r?"together":n==="openrouter"||t.includes("openrouter.ai")?"openrouter":"openai",openRouterRouting:{},vercelGatewayRouting:{},zaiToolStream:!1,supportsStrictMode:!c&&!r&&!o,cacheControlFormat:R,sendSessionAffinityHeaders:!1,supportsLongCacheRetention:!(r||u||o)}}function q(e){const n=Re(e);return e.compat?{supportsStore:e.compat.supportsStore??n.supportsStore,supportsDeveloperRole:e.compat.supportsDeveloperRole??n.supportsDeveloperRole,supportsReasoningEffort:e.compat.supportsReasoningEffort??n.supportsReasoningEffort,supportsUsageInStreaming:e.compat.supportsUsageInStreaming??n.supportsUsageInStreaming,maxTokensField:e.compat.maxTokensField??n.maxTokensField,requiresToolResultName:e.compat.requiresToolResultName??n.requiresToolResultName,requiresAssistantAfterToolResult:e.compat.requiresAssistantAfterToolResult??n.requiresAssistantAfterToolResult,requiresThinkingAsText:e.compat.requiresThinkingAsText??n.requiresThinkingAsText,requiresReasoningContentOnAssistantMessages:e.compat.requiresReasoningContentOnAssistantMessages??n.requiresReasoningContentOnAssistantMessages,thinkingFormat:e.compat.thinkingFormat??n.thinkingFormat,openRouterRouting:e.compat.openRouterRouting??{},vercelGatewayRouting:e.compat.vercelGatewayRouting??n.vercelGatewayRouting,zaiToolStream:e.compat.zaiToolStream??n.zaiToolStream,supportsStrictMode:e.compat.supportsStrictMode??n.supportsStrictMode,cacheControlFormat:e.compat.cacheControlFormat??n.cacheControlFormat,sendSessionAffinityHeaders:e.compat.sendSessionAffinityHeaders??n.sendSessionAffinityHeaders,supportsLongCacheRetention:e.compat.supportsLongCacheRetention??n.supportsLongCacheRetention}:n}export{me as convertMessages,ae as streamOpenAICompletions,Se as streamSimpleOpenAICompletions};
|
dist/assets/openai-responses-Cl3X-tw0.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-BGwNxTBx.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-CbKiZ_Nn.js";import{b as L}from"./transform-messages-Bxe2XUr4.js";import"./hash-DMDecQcg.js";import"./json-parse-CyZFb4VN.js";import"./sanitize-unicode-B62XMGgN.js";var u={};const M=new Set(["openai","openai-codex","opencode"]);function g(e){return e||(typeof process<"u"&&u.PI_CACHE_RETENTION==="long"?"long":"short")}function m(e){return{sendSessionIdHeader:e.compat?.sendSessionIdHeader??!0,supportsLongCacheRetention:e.compat?.supportsLongCacheRetention??!0}}function N(e,t){return t==="long"&&e.supportsLongCacheRetention?"24h":void 0}function x(e){if(e instanceof Error){const t=e.status,r=typeof t=="number"?t:void 0;return r!==void 0?`OpenAI API error (${r}): ${e.message}`:e.message}try{return JSON.stringify(e)}catch{return String(e)}}const H=(e,t,r)=>{const s=new I;return(async()=>{const a={role:"assistant",content:[],api:e.api,provider:e.provider,model:e.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()};try{const o=r?.apiKey||h(e.provider)||"",i=g(r?.cacheRetention)==="none"?void 0:r?.sessionId,p=K(e,t,o,r?.headers,i);let c=q(e,t,r);const f=await r?.onPayload?.(c,e);f!==void 0&&(c=f);const l={...r?.signal?{signal:r.signal}:{},...r?.timeoutMs!==void 0?{timeout:r.timeoutMs}:{},...r?.maxRetries!==void 0?{maxRetries:r.maxRetries}:{}},{data:v,response:d}=await p.responses.create(c,l).withResponse();if(await r?.onResponse?.({status:d.status,headers:w(d.headers)},e),s.push({type:"start",partial:a}),await C(v,a,s,e,{serviceTier:r?.serviceTier,applyServiceTierPricing:(R,y)=>W(R,y,e)}),r?.signal?.aborted)throw new Error("Request was aborted");if(a.stopReason==="aborted"||a.stopReason==="error")throw new Error("An unknown error occurred");s.push({type:"done",reason:a.stopReason,message:a}),s.end()}catch(o){for(const n of a.content)delete n.index,delete n.partialJson;a.stopReason=r?.signal?.aborted?"aborted":"error",a.errorMessage=x(o),s.push({type:"error",reason:a.stopReason,error:a}),s.end()}})(),s},Q=(e,t,r)=>{const s=r?.apiKey||h(e.provider);if(!s)throw new Error(`No API key for provider: ${e.provider}`);const a=L(e,r,s),o=r?.reasoning?E(e,r.reasoning):void 0;return H(e,t,{...a,reasoningEffort:o==="off"?void 0:o})};function K(e,t,r,s,a){if(!r){if(!u.OPENAI_API_KEY)throw new Error("OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.");r=u.OPENAI_API_KEY}const o=m(e),n={...e.headers};if(e.provider==="github-copilot"){const p=_(t.messages),c=O({messages:t.messages,hasImages:p});Object.assign(n,c)}a&&(o.sendSessionIdHeader&&(n.session_id=a),n["x-client-request-id"]=a),s&&Object.assign(n,s);const i=e.provider==="cloudflare-ai-gateway"?{...n,Authorization:n.Authorization??null,"cf-aig-authorization":`Bearer ${r}`}:n;return new A({apiKey:r,baseURL:b(e.provider)?P(e):e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:i})}function q(e,t,r){const s=T(e,t,M),a=g(r?.cacheRetention),o=m(e),n={model:e.id,input:s,stream:!0,prompt_cache_key:a==="none"?void 0:S(r?.sessionId),prompt_cache_retention:N(o,a),store:!1};if(r?.maxTokens&&(n.max_output_tokens=r?.maxTokens),r?.temperature!==void 0&&(n.temperature=r?.temperature),r?.serviceTier!==void 0&&(n.service_tier=r.serviceTier),t.tools&&t.tools.length>0&&(n.tools=k(t.tools)),e.reasoning)if(r?.reasoningEffort||r?.reasoningSummary){const i=r?.reasoningEffort?e.thinkingLevelMap?.[r.reasoningEffort]??r.reasoningEffort:"medium";n.reasoning={effort:i,summary:r?.reasoningSummary||"auto"},n.include=["reasoning.encrypted_content"]}else e.provider!=="github-copilot"&&e.thinkingLevelMap?.off!==null&&(n.reasoning={effort:e.thinkingLevelMap?.off??"none"});return n}function B(e,t){switch(t){case"flex":return .5;case"priority":return e.id==="gpt-5.5"?2.5:2;default:return 1}}function W(e,t,r){const s=B(r,t);s!==1&&(e.cost.input*=s,e.cost.output*=s,e.cost.cacheRead*=s,e.cost.cacheWrite*=s,e.cost.total=e.cost.input+e.cost.output+e.cost.cacheRead+e.cost.cacheWrite)}export{H as streamOpenAIResponses,Q as streamSimpleOpenAIResponses};
|
dist/assets/openai-responses-shared-CbKiZ_Nn.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import{c as S}from"./index-BGwNxTBx.js";import{s as v}from"./hash-DMDecQcg.js";import{a as x}from"./json-parse-CyZFb4VN.js";import{s as _}from"./sanitize-unicode-B62XMGgN.js";import{t as $}from"./transform-messages-Bxe2XUr4.js";function T(r,t){const l={v:1,id:r};return t&&(l.phase=t),JSON.stringify(l)}function C(r){if(r){if(r.startsWith("{"))try{const t=JSON.parse(r);if(t.v===1&&typeof t.id=="string")return t.phase==="commentary"||t.phase==="final_answer"?{id:t.id,phase:t.phase}:{id:t.id}}catch{}return{id:r}}}function M(r,t,l,y){const m=[],i=s=>{const o=s.replace(/[^a-zA-Z0-9_-]/g,"_");return(o.length>64?o.slice(0,64):o).replace(/_+$/,"")},n=s=>{const o=`fc_${v(s)}`;return o.length>64?o.slice(0,64):o},k=(s,o,p)=>{if(!l.has(r.provider)||!s.includes("|"))return i(s);const[h,g]=s.split("|"),f=i(h);let c=p.provider!==r.provider||p.api!==r.api?n(g):i(g);return c.startsWith("fc_")||(c=i(`fc_${c}`)),`${f}|${c}`},d=$(t.messages,r,k);if((y?.includeSystemPrompt??!0)&&t.systemPrompt){const s=r.reasoning?"developer":"system";m.push({role:s,content:_(t.systemPrompt)})}let e=0;for(const s of d){if(s.role==="user")if(typeof s.content=="string")m.push({role:"user",content:[{type:"input_text",text:_(s.content)}]});else{const o=s.content.map(p=>p.type==="text"?{type:"input_text",text:_(p.text)}:{type:"input_image",detail:"auto",image_url:`data:${p.mimeType};base64,${p.data}`});if(o.length===0)continue;m.push({role:"user",content:o})}else if(s.role==="assistant"){const o=[],p=s,h=p.model!==r.id&&p.provider===r.provider&&p.api===r.api;for(const g of s.content)if(g.type==="thinking"){if(g.thinkingSignature){const f=JSON.parse(g.thinkingSignature);o.push(f)}}else if(g.type==="text"){const f=g,u=C(f.textSignature);let c=u?.id;c?c.length>64&&(c=`msg_${v(c)}`):c=`msg_${e}`,o.push({type:"message",role:"assistant",content:[{type:"output_text",text:_(f.text),annotations:[]}],status:"completed",id:c,phase:u?.phase})}else if(g.type==="toolCall"){const f=g,[u,c]=f.id.split("|");let I=c;h&&I?.startsWith("fc_")&&(I=void 0),o.push({type:"function_call",id:I,call_id:u,name:f.name,arguments:JSON.stringify(f.arguments)})}if(o.length===0)continue;m.push(...o)}else if(s.role==="toolResult"){const o=s.content.filter(u=>u.type==="text").map(u=>u.text).join(`
|
| 2 |
+
`),p=s.content.some(u=>u.type==="image"),h=o.length>0,[g]=s.toolCallId.split("|");let f;if(p&&r.input.includes("image")){const u=[];h&&u.push({type:"input_text",text:_(o)});for(const c of s.content)c.type==="image"&&u.push({type:"input_image",detail:"auto",image_url:`data:${c.mimeType};base64,${c.data}`});f=u}else f=_(h?o:"(see attached image)");m.push({type:"function_call_output",call_id:g,output:f})}e++}return m}function W(r,t){const l=t?.strict===void 0?!1:t.strict;return r.map(y=>({type:"function",name:y.name,description:y.description,parameters:y.parameters,strict:l}))}async function N(r,t,l,y,m){let i=null,n=null;const k=t.content,d=()=>k.length-1;for await(const a of r)if(a.type==="response.created")t.responseId=a.response.id;else if(a.type==="response.output_item.added"){const e=a.item;e.type==="reasoning"?(i=e,n={type:"thinking",thinking:""},t.content.push(n),l.push({type:"thinking_start",contentIndex:d(),partial:t})):e.type==="message"?(i=e,n={type:"text",text:""},t.content.push(n),l.push({type:"text_start",contentIndex:d(),partial:t})):e.type==="function_call"&&(i=e,n={type:"toolCall",id:`${e.call_id}|${e.id}`,name:e.name,arguments:{},partialJson:e.arguments||""},t.content.push(n),l.push({type:"toolcall_start",contentIndex:d(),partial:t}))}else if(a.type==="response.reasoning_summary_part.added")i&&i.type==="reasoning"&&(i.summary=i.summary||[],i.summary.push(a.part));else if(a.type==="response.reasoning_summary_text.delta"){if(i?.type==="reasoning"&&n?.type==="thinking"){i.summary=i.summary||[];const e=i.summary[i.summary.length-1];e&&(n.thinking+=a.delta,e.text+=a.delta,l.push({type:"thinking_delta",contentIndex:d(),delta:a.delta,partial:t}))}}else if(a.type==="response.reasoning_summary_part.done"){if(i?.type==="reasoning"&&n?.type==="thinking"){i.summary=i.summary||[];const e=i.summary[i.summary.length-1];e&&(n.thinking+=`
|
| 3 |
+
|
| 4 |
+
`,e.text+=`
|
| 5 |
+
|
| 6 |
+
`,l.push({type:"thinking_delta",contentIndex:d(),delta:`
|
| 7 |
+
|
| 8 |
+
`,partial:t}))}}else if(a.type==="response.reasoning_text.delta")i?.type==="reasoning"&&n?.type==="thinking"&&(n.thinking+=a.delta,l.push({type:"thinking_delta",contentIndex:d(),delta:a.delta,partial:t}));else if(a.type==="response.content_part.added")i?.type==="message"&&(i.content=i.content||[],(a.part.type==="output_text"||a.part.type==="refusal")&&i.content.push(a.part));else if(a.type==="response.output_text.delta"){if(i?.type==="message"&&n?.type==="text"){if(!i.content||i.content.length===0)continue;const e=i.content[i.content.length-1];e?.type==="output_text"&&(n.text+=a.delta,e.text+=a.delta,l.push({type:"text_delta",contentIndex:d(),delta:a.delta,partial:t}))}}else if(a.type==="response.refusal.delta"){if(i?.type==="message"&&n?.type==="text"){if(!i.content||i.content.length===0)continue;const e=i.content[i.content.length-1];e?.type==="refusal"&&(n.text+=a.delta,e.refusal+=a.delta,l.push({type:"text_delta",contentIndex:d(),delta:a.delta,partial:t}))}}else if(a.type==="response.function_call_arguments.delta")i?.type==="function_call"&&n?.type==="toolCall"&&(n.partialJson+=a.delta,n.arguments=x(n.partialJson),l.push({type:"toolcall_delta",contentIndex:d(),delta:a.delta,partial:t}));else if(a.type==="response.function_call_arguments.done"){if(i?.type==="function_call"&&n?.type==="toolCall"){const e=n.partialJson;if(n.partialJson=a.arguments,n.arguments=x(n.partialJson),a.arguments.startsWith(e)){const s=a.arguments.slice(e.length);s.length>0&&l.push({type:"toolcall_delta",contentIndex:d(),delta:s,partial:t})}}}else if(a.type==="response.output_item.done"){const e=a.item;if(e.type==="reasoning"&&n?.type==="thinking"){const s=e.summary?.map(p=>p.text).join(`
|
| 9 |
+
|
| 10 |
+
`)||"",o=e.content?.map(p=>p.text).join(`
|
| 11 |
+
|
| 12 |
+
`)||"";n.thinking=s||o||n.thinking,n.thinkingSignature=JSON.stringify(e),l.push({type:"thinking_end",contentIndex:d(),content:n.thinking,partial:t}),n=null}else if(e.type==="message"&&n?.type==="text")n.text=e.content.map(s=>s.type==="output_text"?s.text:s.refusal).join(""),n.textSignature=T(e.id,e.phase??void 0),l.push({type:"text_end",contentIndex:d(),content:n.text,partial:t}),n=null;else if(e.type==="function_call"){const s=n?.type==="toolCall"&&n.partialJson?x(n.partialJson):x(e.arguments||"{}");let o;n?.type==="toolCall"?(n.arguments=s,delete n.partialJson,o=n):o={type:"toolCall",id:`${e.call_id}|${e.id}`,name:e.name,arguments:s},n=null,l.push({type:"toolcall_end",contentIndex:d(),toolCall:o,partial:t})}}else if(a.type==="response.completed"){const e=a.response;if(e?.id&&(t.responseId=e.id),e?.usage){const s=e.usage.input_tokens_details?.cached_tokens||0;t.usage={input:(e.usage.input_tokens||0)-s,output:e.usage.output_tokens||0,cacheRead:s,cacheWrite:0,totalTokens:e.usage.total_tokens||0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}}}if(S(y,t.usage),m?.applyServiceTierPricing){const s=m.resolveServiceTier?m.resolveServiceTier(e?.service_tier,m.serviceTier):e?.service_tier??m.serviceTier;m.applyServiceTierPricing(t.usage,s)}t.stopReason=J(e?.status),t.content.some(s=>s.type==="toolCall")&&t.stopReason==="stop"&&(t.stopReason="toolUse")}else{if(a.type==="error")throw new Error(`Error Code ${a.code}: ${a.message}`||"Unknown error");if(a.type==="response.failed"){const e=a.response?.error,s=a.response?.incomplete_details,o=e?`${e.code||"unknown"}: ${e.message||"no message"}`:s?.reason?`incomplete: ${s.reason}`:"Unknown error (no error details in response)";throw new Error(o)}}}function J(r){if(!r)return"stop";switch(r){case"completed":return"stop";case"incomplete":return"length";case"failed":case"cancelled":return"error";case"in_progress":case"queued":return"stop";default:{const t=r;throw new Error(`Unhandled stop reason: ${t}`)}}}export{W as a,M as c,N as p};
|
dist/assets/openrouter-BdCuhyTj.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
import{O as y}from"./client-Sq9UPDIz.js";import{g as R}from"./index-BGwNxTBx.js";import{h as x}from"./headers-CgnjaPPL.js";import{s as _}from"./sanitize-unicode-B62XMGgN.js";const O=async(e,r,t)=>{const a={api:e.api,provider:e.provider,model:e.id,output:[],stopReason:"stop",timestamp:Date.now()};try{const s=t?.apiKey||R(e.provider);if(!s)throw new Error(`No API key available for provider: ${e.provider}`);const c=k(e,s,t?.headers);let n=w(e,r);const i=await t?.onPayload?.(n,e);i!==void 0&&(n=i);const o={...t?.signal?{signal:t.signal}:{},...t?.timeoutMs!==void 0?{timeout:t.timeoutMs}:{},...t?.maxRetries!==void 0?{maxRetries:t.maxRetries}:{}},{data:l,response:h}=await c.chat.completions.create(n,o).withResponse();await t?.onResponse?.({status:h.status,headers:x(h.headers)},e);const u=l;a.responseId=u.id,u.usage&&(a.usage=b(u.usage,e));const p=u.choices[0];if(p){const m=p.message.content;typeof m=="string"&&m.length>0&&a.output.push({type:"text",text:m});for(const d of p.message.images??[]){const f=typeof d.image_url=="string"?d.image_url:d.image_url?.url;if(!f?.startsWith("data:"))continue;const g=f.match(/^data:([^;]+);base64,(.+)$/);g&&a.output.push({type:"image",mimeType:g[1],data:g[2]})}}return a}catch(s){return a.stopReason=t?.signal?.aborted?"aborted":"error",a.errorMessage=s instanceof Error?s.message:JSON.stringify(s),a}};function k(e,r,t){return new y({apiKey:r,baseURL:e.baseUrl,dangerouslyAllowBrowser:!0,defaultHeaders:{...e.headers,...t}})}function w(e,r){const t=r.input.map(a=>a.type==="text"?{type:"text",text:_(a.text)}:{type:"image_url",image_url:{url:`data:${a.mimeType};base64,${a.data}`}});return{model:e.id,messages:[{role:"user",content:t}],stream:!1,modalities:e.output.includes("text")?["image","text"]:["image"]}}function b(e,r){const t=e.prompt_tokens||0,a=e.prompt_tokens_details?.cached_tokens||0,s=e.prompt_tokens_details?.cache_write_tokens||0,c=s>0?Math.max(0,a-s):a,n=Math.max(0,t-c-s),i=e.completion_tokens||0,o={input:n,output:i,cacheRead:c,cacheWrite:s,totalTokens:n+i+c+s,cost:{input:r.cost.input/1e6*n,output:r.cost.output/1e6*i,cacheRead:r.cost.cacheRead/1e6*c,cacheWrite:r.cost.cacheWrite/1e6*s,total:0}};return o.cost.total=o.cost.input+o.cost.output+o.cost.cacheRead+o.cost.cacheWrite,o}export{O as generateImagesOpenRouter};
|
dist/index.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
<title>MiniCPM5 Pi Web Agent</title>
|
| 7 |
-
<script type="module" crossorigin src="/assets/index-
|
| 8 |
<link rel="stylesheet" crossorigin href="/assets/index-4NYd9uAS.css">
|
| 9 |
</head>
|
| 10 |
<body>
|
|
@@ -55,7 +55,7 @@
|
|
| 55 |
<div class="compact-grid">
|
| 56 |
<label>
|
| 57 |
Max tokens
|
| 58 |
-
<input id="max-new-tokens" type="number" min="16" max="
|
| 59 |
</label>
|
| 60 |
<label>
|
| 61 |
Temperature
|
|
|
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
<title>MiniCPM5 Pi Web Agent</title>
|
| 7 |
+
<script type="module" crossorigin src="/assets/index-BGwNxTBx.js"></script>
|
| 8 |
<link rel="stylesheet" crossorigin href="/assets/index-4NYd9uAS.css">
|
| 9 |
</head>
|
| 10 |
<body>
|
|
|
|
| 55 |
<div class="compact-grid">
|
| 56 |
<label>
|
| 57 |
Max tokens
|
| 58 |
+
<input id="max-new-tokens" type="number" min="16" max="2048" step="1" value="512" />
|
| 59 |
</label>
|
| 60 |
<label>
|
| 61 |
Temperature
|
index.html
CHANGED
|
@@ -53,7 +53,7 @@
|
|
| 53 |
<div class="compact-grid">
|
| 54 |
<label>
|
| 55 |
Max tokens
|
| 56 |
-
<input id="max-new-tokens" type="number" min="16" max="
|
| 57 |
</label>
|
| 58 |
<label>
|
| 59 |
Temperature
|
|
|
|
| 53 |
<div class="compact-grid">
|
| 54 |
<label>
|
| 55 |
Max tokens
|
| 56 |
+
<input id="max-new-tokens" type="number" min="16" max="2048" step="1" value="512" />
|
| 57 |
</label>
|
| 58 |
<label>
|
| 59 |
Temperature
|
package.json
CHANGED
|
@@ -6,6 +6,7 @@
|
|
| 6 |
"dev": "vite --host 0.0.0.0",
|
| 7 |
"build": "vite build",
|
| 8 |
"preview": "vite preview --host 0.0.0.0",
|
|
|
|
| 9 |
"smoke:local-model": "node scripts/smoke_local_model_web_agent.mjs",
|
| 10 |
"smoke:web": "node scripts/smoke_web_agent.mjs",
|
| 11 |
"verify": "node scripts/verify_tjs_model.mjs"
|
|
|
|
| 6 |
"dev": "vite --host 0.0.0.0",
|
| 7 |
"build": "vite build",
|
| 8 |
"preview": "vite preview --host 0.0.0.0",
|
| 9 |
+
"probe:tokens": "node scripts/probe_token_budgets.mjs",
|
| 10 |
"smoke:local-model": "node scripts/smoke_local_model_web_agent.mjs",
|
| 11 |
"smoke:web": "node scripts/smoke_web_agent.mjs",
|
| 12 |
"verify": "node scripts/verify_tjs_model.mjs"
|
scripts/probe_token_budgets.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { chromium } from "@playwright/test";
|
| 2 |
+
|
| 3 |
+
const baseUrl = process.argv[2] || "http://localhost:5173/?device=wasm";
|
| 4 |
+
const budgets = (process.env.TOKEN_BUDGETS || "512,1024,2048")
|
| 5 |
+
.split(",")
|
| 6 |
+
.map((value) => value.trim())
|
| 7 |
+
.filter(Boolean);
|
| 8 |
+
const executablePath = process.env.CHROMIUM_PATH || "/snap/bin/chromium";
|
| 9 |
+
|
| 10 |
+
const results = [];
|
| 11 |
+
|
| 12 |
+
for (const budget of budgets) {
|
| 13 |
+
let browser;
|
| 14 |
+
let page;
|
| 15 |
+
const consoleLines = [];
|
| 16 |
+
const result = { budget, ok: false };
|
| 17 |
+
|
| 18 |
+
try {
|
| 19 |
+
browser = await chromium.launch({
|
| 20 |
+
executablePath,
|
| 21 |
+
headless: true,
|
| 22 |
+
args: ["--no-sandbox", "--disable-dev-shm-usage"],
|
| 23 |
+
});
|
| 24 |
+
page = await browser.newPage();
|
| 25 |
+
page.setDefaultTimeout(1200000);
|
| 26 |
+
page.on("console", (message) => {
|
| 27 |
+
consoleLines.push(`${message.type()}: ${message.text()}`);
|
| 28 |
+
});
|
| 29 |
+
page.on("pageerror", (error) => {
|
| 30 |
+
consoleLines.push(`pageerror: ${error.stack || error.message}`);
|
| 31 |
+
});
|
| 32 |
+
page.on("crash", () => {
|
| 33 |
+
consoleLines.push("page crash");
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
const started = Date.now();
|
| 37 |
+
await page.goto(baseUrl, { waitUntil: "networkidle" });
|
| 38 |
+
await page.waitForFunction(() => document.querySelector("#status")?.textContent === "Ready");
|
| 39 |
+
await page.fill("#max-new-tokens", budget);
|
| 40 |
+
await page.fill("#temperature", "0");
|
| 41 |
+
await page.selectOption("#gate-device", "wasm");
|
| 42 |
+
await page.click("#confirm-load-model");
|
| 43 |
+
await page.waitForFunction(() => window.__piWebAgent?.modelReady === true, null, {
|
| 44 |
+
timeout: 600000,
|
| 45 |
+
});
|
| 46 |
+
result.loadMs = Date.now() - started;
|
| 47 |
+
|
| 48 |
+
const runStarted = Date.now();
|
| 49 |
+
await page.click("#send");
|
| 50 |
+
await page.waitForFunction(() => document.querySelector("#status")?.textContent === "Agent running", null, {
|
| 51 |
+
timeout: 120000,
|
| 52 |
+
});
|
| 53 |
+
await page.waitForFunction(() => document.querySelector("#status")?.textContent === "Ready", null, {
|
| 54 |
+
timeout: 1200000,
|
| 55 |
+
});
|
| 56 |
+
await page.waitForFunction(() => window.__piWebAgent?.transcript?.includes("pi sandbox result: 42"), null, {
|
| 57 |
+
timeout: 120000,
|
| 58 |
+
});
|
| 59 |
+
result.runMs = Date.now() - runStarted;
|
| 60 |
+
|
| 61 |
+
const transcript = await page.evaluate(() => window.__piWebAgent?.transcript || "");
|
| 62 |
+
const files = await page.textContent("#files");
|
| 63 |
+
result.modelStatus = await page.textContent("#model-status");
|
| 64 |
+
result.ok = transcript.includes("pi sandbox result: 42") && files?.includes("hello.js");
|
| 65 |
+
result.transcriptChars = transcript.length;
|
| 66 |
+
} catch (error) {
|
| 67 |
+
result.error = error instanceof Error ? error.message : String(error);
|
| 68 |
+
} finally {
|
| 69 |
+
result.warnings = consoleLines.filter((line) => line.startsWith("warning:") || line === "page crash").slice(-3);
|
| 70 |
+
results.push(result);
|
| 71 |
+
await page?.close().catch(() => {});
|
| 72 |
+
await browser?.close().catch(() => {});
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
console.log(JSON.stringify(results, null, 2));
|
| 77 |
+
if (results.some((result) => !result.ok)) {
|
| 78 |
+
throw new Error("At least one token budget probe failed.");
|
| 79 |
+
}
|
scripts/smoke_local_model_web_agent.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { chromium } from "@playwright/test";
|
|
| 2 |
|
| 3 |
const baseUrl = process.argv[2] || "http://localhost:5173/?device=wasm";
|
| 4 |
const executablePath = process.env.CHROMIUM_PATH || "/snap/bin/chromium";
|
|
|
|
| 5 |
|
| 6 |
const browser = await chromium.launch({
|
| 7 |
executablePath,
|
|
@@ -26,7 +27,7 @@ try {
|
|
| 26 |
const isolated = await page.evaluate(() => globalThis.crossOriginIsolated);
|
| 27 |
if (!isolated) throw new Error("Page is not cross-origin isolated.");
|
| 28 |
|
| 29 |
-
await page.fill("#max-new-tokens",
|
| 30 |
await page.fill("#temperature", "0");
|
| 31 |
await page.selectOption("#gate-device", "wasm");
|
| 32 |
await page.click("#confirm-load-model");
|
|
@@ -61,7 +62,7 @@ try {
|
|
| 61 |
throw new Error(`Expected MiniCPM model status to be ready, got: ${modelStatus}`);
|
| 62 |
}
|
| 63 |
|
| 64 |
-
console.log(JSON.stringify({ ok: true, isolated, modelStatus, transcript, files }, null, 2));
|
| 65 |
} catch (error) {
|
| 66 |
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
| 67 |
throw error;
|
|
|
|
| 2 |
|
| 3 |
const baseUrl = process.argv[2] || "http://localhost:5173/?device=wasm";
|
| 4 |
const executablePath = process.env.CHROMIUM_PATH || "/snap/bin/chromium";
|
| 5 |
+
const maxNewTokens = process.env.MAX_NEW_TOKENS || "512";
|
| 6 |
|
| 7 |
const browser = await chromium.launch({
|
| 8 |
executablePath,
|
|
|
|
| 27 |
const isolated = await page.evaluate(() => globalThis.crossOriginIsolated);
|
| 28 |
if (!isolated) throw new Error("Page is not cross-origin isolated.");
|
| 29 |
|
| 30 |
+
await page.fill("#max-new-tokens", maxNewTokens);
|
| 31 |
await page.fill("#temperature", "0");
|
| 32 |
await page.selectOption("#gate-device", "wasm");
|
| 33 |
await page.click("#confirm-load-model");
|
|
|
|
| 62 |
throw new Error(`Expected MiniCPM model status to be ready, got: ${modelStatus}`);
|
| 63 |
}
|
| 64 |
|
| 65 |
+
console.log(JSON.stringify({ ok: true, isolated, maxNewTokens, modelStatus, transcript, files }, null, 2));
|
| 66 |
} catch (error) {
|
| 67 |
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
| 68 |
throw error;
|
src/piAgent.js
CHANGED
|
@@ -18,8 +18,8 @@ const LOCAL_MODEL = {
|
|
| 18 |
cacheRead: 0,
|
| 19 |
cacheWrite: 0,
|
| 20 |
},
|
| 21 |
-
contextWindow:
|
| 22 |
-
maxTokens:
|
| 23 |
};
|
| 24 |
|
| 25 |
const EMPTY_USAGE = {
|
|
@@ -82,9 +82,7 @@ function buildPrompt(context) {
|
|
| 82 |
})
|
| 83 |
.join("\n\n");
|
| 84 |
|
| 85 |
-
return `
|
| 86 |
-
|
| 87 |
-
You are running as a pi Agent inside a browser-only app. The sandbox is a WebContainer: it has a virtual filesystem and can spawn browser-contained Node.js processes.
|
| 88 |
|
| 89 |
Use tools by returning strict JSON only. Do not use markdown.
|
| 90 |
|
|
@@ -230,7 +228,7 @@ function emitFinal(stream, message, text = "") {
|
|
| 230 |
export function createPiAgent({ sandbox, modelMode, device, maxTokens, temperature, onModelStatus = () => {} }) {
|
| 231 |
env.allowLocalModels = false;
|
| 232 |
env.allowRemoteModels = true;
|
| 233 |
-
env.backends.onnx.wasm.numThreads = Math.min(
|
| 234 |
|
| 235 |
let generatorPromise = null;
|
| 236 |
let generatorKey = "";
|
|
|
|
| 18 |
cacheRead: 0,
|
| 19 |
cacheWrite: 0,
|
| 20 |
},
|
| 21 |
+
contextWindow: 131072,
|
| 22 |
+
maxTokens: 2048,
|
| 23 |
};
|
| 24 |
|
| 25 |
const EMPTY_USAGE = {
|
|
|
|
| 82 |
})
|
| 83 |
.join("\n\n");
|
| 84 |
|
| 85 |
+
return `You are running as a pi Agent inside a browser-only app. The sandbox is a WebContainer: it has a virtual filesystem and can spawn browser-contained Node.js processes.
|
|
|
|
|
|
|
| 86 |
|
| 87 |
Use tools by returning strict JSON only. Do not use markdown.
|
| 88 |
|
|
|
|
| 228 |
export function createPiAgent({ sandbox, modelMode, device, maxTokens, temperature, onModelStatus = () => {} }) {
|
| 229 |
env.allowLocalModels = false;
|
| 230 |
env.allowRemoteModels = true;
|
| 231 |
+
env.backends.onnx.wasm.numThreads = Math.min(2, navigator.hardwareConcurrency || 2);
|
| 232 |
|
| 233 |
let generatorPromise = null;
|
| 234 |
let generatorKey = "";
|