@ohtaman/washi 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.ja.md +60 -23
  2. package/README.md +59 -22
  3. package/README.zh-CN.md +62 -25
  4. package/THIRD_PARTY_LICENSES.md +34 -1
  5. package/dist/browser/solvers/glpk.js +289 -238
  6. package/dist/browser/solvers/glpk.min.js +1 -1
  7. package/dist/browser/solvers/highs-__vite-browser-external-Dn-83hnF.js +7 -0
  8. package/dist/browser/solvers/highs-browser-runtime-0Bgq5ksG.js +2021 -0
  9. package/dist/browser/{loader-DewlSho8.js → solvers/highs-loader-BLY3wzDH.js} +190 -149
  10. package/dist/browser/solvers/highs-worker.js +54 -0
  11. package/dist/browser/solvers/highs.js +1414 -0
  12. package/dist/browser/solvers/highs.wasm +0 -0
  13. package/dist/browser/solvers/pumpkin-_washi-pumpkin-node-runtime-CEKUDK6d.js +9 -0
  14. package/dist/browser/solvers/pumpkin-loader-CvdaKxf4.js +320 -0
  15. package/dist/browser/solvers/pumpkin-worker.js +45 -0
  16. package/dist/browser/solvers/pumpkin.js +1777 -0
  17. package/dist/browser/solvers/pumpkin.wasm +0 -0
  18. package/dist/browser/solvers/pumpkin.wasm.LICENSE +21 -0
  19. package/dist/browser/solvers/z3.js +1600 -1335
  20. package/dist/browser/solvers/z3.min.js +2 -2
  21. package/dist/browser/washi.js +3313 -1699
  22. package/dist/browser/washi.min.js +5 -3
  23. package/dist/lib/solvers/__vite-browser-external-oLchLuMx.js +7 -0
  24. package/dist/lib/solvers/glpk.js +174 -132
  25. package/dist/lib/solvers/highs-browser-runtime.js +2413 -0
  26. package/dist/lib/{highs-node-runtime.js → solvers/highs-node-runtime.js} +1 -1
  27. package/dist/lib/solvers/highs-runtime.js +16 -0
  28. package/dist/lib/solvers/highs-worker.js +54 -0
  29. package/dist/lib/solvers/highs.js +410 -0
  30. package/dist/lib/solvers/highs.wasm +0 -0
  31. package/dist/lib/{loader-BZUQNvh4.js → solvers/loader-CDpPXN_F.js} +138 -116
  32. package/dist/lib/solvers/loader-Ow74Fa1B.js +311 -0
  33. package/dist/lib/solvers/lp-writer-CLScMFbS.js +115 -0
  34. package/dist/lib/solvers/pumpkin-assets.js +1 -0
  35. package/dist/lib/solvers/pumpkin-node-runtime.js +15 -0
  36. package/dist/lib/solvers/pumpkin-worker-factory.js +1 -0
  37. package/dist/lib/solvers/pumpkin-worker.js +45 -0
  38. package/dist/lib/solvers/pumpkin.js +868 -0
  39. package/dist/lib/solvers/pumpkin.wasm +0 -0
  40. package/dist/lib/solvers/pumpkin.wasm.LICENSE +21 -0
  41. package/dist/lib/solvers/registry-CrYRRk9q.js +907 -0
  42. package/dist/lib/solvers/rolldown-runtime-DtPi1Y-2.js +13 -0
  43. package/dist/lib/solvers/types-BGqPkxwu.js +12 -0
  44. package/dist/lib/solvers/z3.js +489 -307
  45. package/dist/lib/washi.js +3905 -1410
  46. package/dist/types/decomposition/benders.d.ts.map +1 -1
  47. package/dist/types/index.d.ts +4 -5
  48. package/dist/types/index.d.ts.map +1 -1
  49. package/dist/types/modeler/boolean-expr.d.ts +137 -29
  50. package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
  51. package/dist/types/modeler/constraint.d.ts +40 -4
  52. package/dist/types/modeler/constraint.d.ts.map +1 -1
  53. package/dist/types/modeler/expression.d.ts +129 -2
  54. package/dist/types/modeler/expression.d.ts.map +1 -1
  55. package/dist/types/modeler/index.d.ts +4 -6
  56. package/dist/types/modeler/index.d.ts.map +1 -1
  57. package/dist/types/modeler/linearizer.d.ts +11 -2
  58. package/dist/types/modeler/linearizer.d.ts.map +1 -1
  59. package/dist/types/modeler/lp-writer.d.ts.map +1 -1
  60. package/dist/types/modeler/model.d.ts +86 -126
  61. package/dist/types/modeler/model.d.ts.map +1 -1
  62. package/dist/types/modeler/parser.d.ts.map +1 -1
  63. package/dist/types/modeler/quad-expr.d.ts +8 -2
  64. package/dist/types/modeler/quad-expr.d.ts.map +1 -1
  65. package/dist/types/modeler/solution-validator.d.ts +12 -12
  66. package/dist/types/modeler/solution-validator.d.ts.map +1 -1
  67. package/dist/types/modeler/solvers/glpk/loader.d.ts +4 -0
  68. package/dist/types/modeler/solvers/glpk/loader.d.ts.map +1 -1
  69. package/dist/types/modeler/solvers/glpk/solver.d.ts +2 -2
  70. package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
  71. package/dist/types/modeler/solvers/highs/driver.d.ts.map +1 -1
  72. package/dist/types/modeler/solvers/highs/loader.d.ts +8 -6
  73. package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
  74. package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
  75. package/dist/types/modeler/solvers/highs/worker-client.d.ts +16 -0
  76. package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
  77. package/dist/types/modeler/solvers/index.d.ts +2 -1
  78. package/dist/types/modeler/solvers/index.d.ts.map +1 -1
  79. package/dist/types/modeler/solvers/mock.d.ts +2 -3
  80. package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
  81. package/dist/types/modeler/solvers/pumpkin/assets.d.ts +2 -0
  82. package/dist/types/modeler/solvers/pumpkin/assets.d.ts.map +1 -0
  83. package/dist/types/modeler/solvers/pumpkin/index.d.ts +6 -0
  84. package/dist/types/modeler/solvers/pumpkin/index.d.ts.map +1 -0
  85. package/dist/types/modeler/solvers/pumpkin/loader.d.ts +40 -0
  86. package/dist/types/modeler/solvers/pumpkin/loader.d.ts.map +1 -0
  87. package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts +3 -0
  88. package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts.map +1 -0
  89. package/dist/types/modeler/solvers/pumpkin/solver.d.ts +23 -0
  90. package/dist/types/modeler/solvers/pumpkin/solver.d.ts.map +1 -0
  91. package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts +74 -0
  92. package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts.map +1 -0
  93. package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts +3 -0
  94. package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts.map +1 -0
  95. package/dist/types/modeler/solvers/pumpkin/worker.d.ts +7 -0
  96. package/dist/types/modeler/solvers/pumpkin/worker.d.ts.map +1 -0
  97. package/dist/types/modeler/solvers/pumpkin.d.ts +2 -0
  98. package/dist/types/modeler/solvers/pumpkin.d.ts.map +1 -0
  99. package/dist/types/modeler/solvers/types.d.ts +7 -9
  100. package/dist/types/modeler/solvers/types.d.ts.map +1 -1
  101. package/dist/types/modeler/solvers/z3/loader.d.ts +4 -0
  102. package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
  103. package/dist/types/modeler/solvers/z3/solver.d.ts +2 -15
  104. package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
  105. package/dist/types/modeler/types.d.ts +12 -15
  106. package/dist/types/modeler/types.d.ts.map +1 -1
  107. package/dist/types/modeler/validation.d.ts +26 -2
  108. package/dist/types/modeler/validation.d.ts.map +1 -1
  109. package/dist/types/modeler/variable.d.ts +9 -5
  110. package/dist/types/modeler/variable.d.ts.map +1 -1
  111. package/docs/examples/diet-sensitivity.md +33 -14
  112. package/docs/examples/ecommerce-coupons.md +17 -9
  113. package/docs/examples/index.md +36 -27
  114. package/docs/examples/knapsack.md +11 -5
  115. package/docs/examples/portfolio-qp.md +21 -8
  116. package/docs/examples/production.md +10 -5
  117. package/docs/examples/project-scheduling.md +119 -0
  118. package/docs/examples/shift-scheduling.md +29 -18
  119. package/docs/examples/smt-security.md +27 -11
  120. package/docs/examples/transportation.md +23 -13
  121. package/docs/examples/traveling-salesperson.md +34 -22
  122. package/docs/examples/unsat-core.md +27 -14
  123. package/docs/examples/vehicle-routing.md +74 -28
  124. package/docs/getting-started/ai-agents.md +190 -82
  125. package/docs/getting-started/coding-agents.md +177 -0
  126. package/docs/getting-started/cross-origin.md +27 -14
  127. package/docs/getting-started/index.md +49 -43
  128. package/docs/getting-started/runtimes.md +94 -40
  129. package/docs/guide/constraint-programming.md +171 -0
  130. package/docs/guide/constraints.md +115 -33
  131. package/docs/guide/decomposition.md +118 -88
  132. package/docs/guide/expressions.md +106 -43
  133. package/docs/guide/formulation-tips.md +47 -28
  134. package/docs/guide/objectives.md +102 -75
  135. package/docs/guide/unsat-core.md +62 -50
  136. package/docs/guide/variables.md +81 -35
  137. package/docs/solvers/custom-solver.md +163 -122
  138. package/docs/solvers/glpk.md +58 -17
  139. package/docs/solvers/highs.md +124 -57
  140. package/docs/solvers/index.md +33 -20
  141. package/docs/solvers/pumpkin.md +171 -0
  142. package/docs/solvers/z3.md +63 -25
  143. package/llms-full.txt +2232 -932
  144. package/llms.txt +144 -91
  145. package/package.json +55 -19
  146. package/skills/washi/SKILL.md +107 -34
  147. package/dist/browser/_washi-node-only-BvRk9kiK.js +0 -0
  148. package/dist/browser/browser-runtime-TLCY0hkc.js +0 -1793
  149. package/dist/browser/highs-worker.js +0 -39
  150. package/dist/browser/highs.wasm +0 -0
  151. package/dist/lib/_washi-node-only-BvRk9kiK.js +0 -0
  152. package/dist/lib/highs-browser-runtime.js +0 -2185
  153. package/dist/lib/highs-runtime.js +0 -2
  154. package/dist/lib/highs-worker.js +0 -39
  155. package/dist/lib/highs.wasm +0 -0
  156. package/dist/lib/registry-D7TZ3Wh_.js +0 -1034
  157. package/docs/ja/getting-started/ai-agents.md +0 -280
  158. package/docs/zh/getting-started/ai-agents.md +0 -280
  159. /package/dist/browser/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
  160. /package/dist/lib/{highs-assets.js → solvers/highs-assets.js} +0 -0
  161. /package/dist/lib/{highs-worker-factory.js → solvers/highs-worker-factory.js} +0 -0
  162. /package/dist/lib/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
@@ -1,31 +1,39 @@
1
1
  # Cross-Origin Isolation (Z3 & Browsers)
2
2
 
3
- When running the **Z3** solver inside a web browser, Cross-Origin Isolation HTTP headers are required so the browser enables `SharedArrayBuffer`.
3
+ Running the Z3 solver in a browser requires two browser security conditions: a **Secure Context** and **Cross-Origin Isolation**. Together, they enable `SharedArrayBuffer` for Z3's multithreaded WebAssembly build.
4
4
 
5
5
  > [!TIP]
6
- > **Not needed for HiGHS, GLPK, or Node.js**: The built-in **HiGHS** solver and **GLPK** plugin run in standard browser contexts without Cross-Origin Isolation or `SharedArrayBuffer`. Running Z3 in **Node.js** also requires no HTTP headers. You only need this page when using `@ohtaman/washi/solvers/z3` in the browser.
6
+ > **Not needed for HiGHS, Pumpkin, GLPK, or Node.js**: Bundled HiGHS and Pumpkin, as well as the GLPK plugin, never use `SharedArrayBuffer` and work in standard browser contexts. Running Z3 in Node.js also requires no HTTP headers. You only need this guide when using Z3 in a browser.
7
7
 
8
8
  ---
9
9
 
10
- ## Why is Cross-Origin Isolation Required?
10
+ ## Why Are These Preconditions Required?
11
11
 
12
- Z3's WebAssembly build uses multithreading (pthreads) backed by `SharedArrayBuffer` and Web Workers. Modern browsers (Chrome, Firefox, Safari, Edge) disable `SharedArrayBuffer` by default to mitigate Spectre-style side-channel attacks unless the top-level document opts into **Cross-Origin Isolation** (`window.crossOriginIsolated === true`).
12
+ Z3's WebAssembly binary uses POSIX threads (`pthreads`) backed by `SharedArrayBuffer` and Web Workers. To mitigate Spectre-style side-channel attacks, modern browsers disable `SharedArrayBuffer` by default.
13
+
14
+ Browsers re-enable `SharedArrayBuffer` only when both conditions hold on the top-level document:
15
+
16
+ 1. **Secure Context**: The page must load over `https://` or `http://localhost` (including `127.0.0.1` and `[::1]`) so `window.isSecureContext === true`. Plain `http://` LAN IP addresses do not qualify.
17
+ 2. **Cross-Origin Isolation**: The document must opt into isolation headers so `window.crossOriginIsolated === true`.
13
18
 
14
19
  ---
15
20
 
16
21
  ## Required HTTP Response Headers
17
22
 
18
- Serve the top-level HTML document with:
23
+ Serve the top-level HTML document with these two response headers:
24
+
25
+ - **Cross-Origin-Opener-Policy (`COOP`)**: `same-origin`
26
+ - **Cross-Origin-Embedder-Policy (`COEP`)**: `require-corp`
19
27
 
20
28
  ```http
21
29
  Cross-Origin-Opener-Policy: same-origin
22
30
  Cross-Origin-Embedder-Policy: require-corp
23
31
  ```
24
32
 
25
- If your page loads cross-origin subresources (such as scripts, images, or WASM files from another domain), those responses must also include `Cross-Origin-Resource-Policy: cross-origin` (or CORS headers).
33
+ If your page loads scripts, images, or `.wasm` files from another origin, those responses must include `Cross-Origin-Resource-Policy: cross-origin` (`CORP`) or valid CORS headers (`Access-Control-Allow-Origin`), depending on the request mode.
26
34
 
27
35
  > [!NOTE]
28
- > Chromium and Firefox also support `Cross-Origin-Embedder-Policy: credentialless`, which relaxes subresource CORP requirements, **but Safari (WebKit) does not support `credentialless`**. Use `require-corp` if you support Safari / iOS.
36
+ > Chromium and Firefox also accept `Cross-Origin-Embedder-Policy: credentialless`, which relaxes `CORP` requirements on cross-origin subresources. **Safari (WebKit) does not support `credentialless`**, so use `require-corp` if you support Safari or iOS.
29
37
 
30
38
  ---
31
39
 
@@ -33,7 +41,7 @@ If your page loads cross-origin subresources (such as scripts, images, or WASM f
33
41
 
34
42
  ### 1. Vite (`vite.config.ts`)
35
43
 
36
- Configure both the local dev server (`vite`) and production preview server (`vite preview`):
44
+ Configure both the `vite` dev server and `vite preview` server:
37
45
 
38
46
  ```typescript
39
47
  import { defineConfig } from "vite";
@@ -56,7 +64,7 @@ export default defineConfig({
56
64
 
57
65
  ### 2. Cloudflare Pages / Netlify (`public/_headers`)
58
66
 
59
- Place a `_headers` file in `public/` so it is copied to the root of your build output:
67
+ Place a `_headers` file in `public/` so your build copies it to the site root:
60
68
 
61
69
  ```text
62
70
  /*
@@ -85,7 +93,7 @@ const nextConfig = {
85
93
  module.exports = nextConfig;
86
94
  ```
87
95
 
88
- ### 4. Custom Node.js / Express Server
96
+ ### 4. Express (Custom Node.js Server)
89
97
 
90
98
  ```javascript
91
99
  app.use((req, res, next) => {
@@ -97,19 +105,24 @@ app.use((req, res, next) => {
97
105
 
98
106
  ### 5. Static Hosting without Header Control (e.g., GitHub Pages)
99
107
 
100
- Platforms like GitHub Pages do not allow customizing HTTP response headers directly. You can use **`coi-serviceworker`** to inject the isolation headers via a client-side Service Worker:
108
+ Platforms like GitHub Pages do not let you customize HTTP response headers. You can use **`coi-serviceworker`** to inject `COOP` and `COEP` headers from a client-side Service Worker. Note that Service Workers themselves also require a Secure Context (`https://` or `http://localhost`).
101
109
 
102
- 1. Place `coi-serviceworker.js` at your site root.
110
+ 1. Copy `coi-serviceworker.js` from [`coi-serviceworker`](https://github.com/gzuidhof/coi-serviceworker) or WaSHI's reference [`public/coi-serviceworker.js`](https://github.com/ohtaman/washi/blob/main/public/coi-serviceworker.js) to your site root.
103
111
  2. Load it in your HTML `<head>`:
104
112
 
105
113
  ```html
106
114
  <script src="/coi-serviceworker.js"></script>
107
115
  ```
108
116
 
109
- On the first visit, the Service Worker registers and reloads the page once to establish `window.crossOriginIsolated === true`.
117
+ On the first visit, the Service Worker registers and reloads the page once so `window.crossOriginIsolated` becomes `true`.
110
118
 
111
119
  ---
112
120
 
113
121
  ## Runtime Check in WaSHI
114
122
 
115
- WaSHI's Z3 adapter checks `crossOriginIsolated` before initializing Z3 in the browser. If Cross-Origin Isolation is missing, `solve()` / `check()` immediately rejects with an actionable error explaining that COOP/COEP headers or a Service Worker are required, rather than hanging silently.
123
+ Before initializing Z3 in a browser, WaSHI validates both preconditions in order:
124
+
125
+ | Check | Failure Condition | Result |
126
+ | --- | --- | --- |
127
+ | **Secure Context** | `!window.isSecureContext` and hostname is not `localhost`, `127.0.0.1`, or `[::1]` | `solve()` / `check()` immediately rejects with an HTTPS or `localhost` requirement error. |
128
+ | **Cross-Origin Isolation** | `SharedArrayBuffer` is missing or `window.crossOriginIsolated !== true` | `solve()` / `check()` immediately rejects listing the required `COOP` and `COEP` headers instead of hanging. |
@@ -1,35 +1,19 @@
1
1
  # Introduction & Installation
2
2
 
3
- **WaSHI** (WebAssembly-backed, Solver-independent, High-level modeling Interface) brings the algebraic modeling style of Python's **PuLP** to the TypeScript and JavaScript ecosystem. Write expressions naturally with tagged templates (`expr`) and solve mathematical optimization problems directly in **Node.js and the browser** using WebAssembly solvers (**HiGHS**, **Z3**, and **GLPK**).
4
-
5
- ```typescript
6
- import { Model, expr, maximize } from "@ohtaman/washi";
7
-
8
- const m = new Model("production");
9
-
10
- // 1. Define decision variables with realistic finite bounds (lb / ub)
11
- const desks = m.integer("desks", { lb: 0, ub: 100 });
12
- const chairs = m.integer("chairs", { lb: 0, ub: 100 });
13
-
14
- // 2. Set the objective function and constraints
15
- m.objective(maximize(expr`30 * ${desks} + 50 * ${chairs}`));
16
- m.constraint(expr`${desks} + 2 * ${chairs} <= 40`, "wood_capacity");
17
- m.constraint(expr`2 * ${desks} + ${chairs} <= 50`, "labor_capacity");
18
-
19
- // 3. Solve (built-in HiGHS WebAssembly) and validate the solution
20
- const status = await m.solve();
21
- if (m.hasPrimalSolution) {
22
- const report = m.validateSolution();
23
- console.log(status, report.valid); // "Optimal", true
24
- console.log(`Profit: ${m.objectiveValue} (desks=${desks.value}, chairs=${chairs.value})`); // 1100 (desks=20, chairs=10)
25
- }
26
- ```
3
+ **WaSHI** (WebAssembly-backed, Solver-independent, High-level modeling Interface) brings Python PuLP-style algebraic modeling to TypeScript and JavaScript. You can write linear, quadratic, and logical expressions with tagged templates (`expr`) and model mathematical optimization and constraint programming problems directly in **Node.js (18+)** and modern browsers.
27
4
 
28
5
  ---
29
6
 
30
7
  ## 📦 Installation
31
8
 
32
- The default **HiGHS** solver (LP / MIP / Convex QP) is bundled and works out of the box:
9
+ Installing `@ohtaman/washi` bundles two zero-dependency solvers out of the box, with two additional solvers available as optional dependencies:
10
+
11
+ | Solver | Install | Problem Classes & Capabilities |
12
+ | --- | --- | --- |
13
+ | **HiGHS** *(default)* | Bundled in `@ohtaman/washi` (no extra import; subpath `@ohtaman/washi/solvers/highs`) | Linear, mixed-integer, and convex quadratic programming — `LP`, `MIP`, and `QP`. |
14
+ | **Pumpkin** | Bundled in `@ohtaman/washi` (`{ solver: "pumpkin" }`, no extra import; subpath `@ohtaman/washi/solvers/pumpkin`) | Constraint Programming (`CP`) and exact `unsatCore` conflict diagnosis on bounded integers and booleans. |
15
+ | **Z3** | Optional (`z3-solver`) | Satisfiability Modulo Theories (`SMT`), continuous logic, and exact `unsatCore` diagnosis (including continuous variables). |
16
+ | **GLPK** | Optional (`glpk.js`) | Alternative `LP` and `MIP` solver under the GPL-3.0 license. |
33
17
 
34
18
  ::: code-group
35
19
 
@@ -51,37 +35,59 @@ bun add @ohtaman/washi
51
35
 
52
36
  :::
53
37
 
54
- Install optional peer dependencies only if you need **Z3** (SMT logic & Unsat Core diagnosis) or **GLPK**:
38
+ Install optional dependencies when you need **Z3** or **GLPK**:
55
39
 
56
40
  ```bash
57
- npm install z3-solver # For SMT logic and Unsat Core conflict diagnosis
58
- npm install glpk.js # For GLPK solver (GPL-3.0)
41
+ npm install z3-solver # Optional: SMT logic and unsatCore diagnosis for models with continuous variables
42
+ npm install glpk.js # Optional: GLPK solver (GPL-3.0)
59
43
  ```
60
44
 
45
+ In browsers, Z3 requires a Secure Context and Cross-Origin Isolation headers — `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy`, abbreviated `COOP`/`COEP`. See [Cross-Origin Isolation](./cross-origin.md) for server setup.
46
+
61
47
  ---
62
48
 
63
- ## 💡 Why TypeScript & WebAssembly?
49
+ ## 🚀 Quick Start
64
50
 
65
- - **Zero native setup across Node.js and the browser**: No platform-specific C/C++ builds or separate Python runtimes to manage. A single `npm install` runs the same modeling code across **Node.js APIs, CLIs, and scripts** as well as in the **browser**.
66
- - **Lightweight embedding with standard Web APIs & UIs**: Go beyond local analysis scripts by shipping **interactive Web tools, mini-games, or Chrome extensions** that anyone can open via a URL with zero backend hosting cost. You can wire mathematical optimization directly into drag-and-drop UIs or **standard browser capabilities**—such as screen/camera capture and speech recognition (Web Speech API)—with no network round-trips or external data transfer.
67
- - **End-to-end type safety from app data to model**: Prevent common indexing and dictionary-key mistakes with TypeScript's type system. Typed variable dictionaries (`Record<K, Var>`) and variable-reference interpolation (`` expr`30 * ${qty.desk} <= 40` ``) connect directly to your application schemas, preserving compile-time variable/key checks, IDE autocompletion, and symbol renaming.
68
- - **Strong synergy with AI development & LLM workflows**: Keeping your application and optimization model in a **single TypeScript codebase** lets you validate everything in one test runner—without crossing a Python service boundary—and deploy easily as static assets or a single service. That tight test-and-deploy loop fits AI-assisted coding naturally, while at runtime you can pair flexible LLM extraction with exact solver feasibility and conflict diagnosis (`unsatCore`) in the same runtime.
51
+ The production-planning model below maximizes profit from desks and chairs under wood and labor limits:
69
52
 
70
- ---
53
+ ```typescript
54
+ import { Model, expr, maximize } from "@ohtaman/washi";
71
55
 
72
- ## 🔍 Core Workflow Essentials
56
+ const m = new Model("production");
57
+
58
+ // 1. Define decision variables with realistic finite bounds (lb / ub)
59
+ const desks = m.integer("desks", { lb: 0, ub: 100 });
60
+ const chairs = m.integer("chairs", { lb: 0, ub: 100 });
61
+
62
+ // 2. Set the objective function and constraints
63
+ m.objective(maximize(expr`30 * ${desks} + 50 * ${chairs}`));
64
+ m.constraint(expr`${desks} + 2 * ${chairs} <= 40`, "wood_capacity");
65
+ m.constraint(expr`2 * ${desks} + ${chairs} <= 50`, "labor_capacity");
73
66
 
74
- Three practical habits cover the essentials shown in the snippet above:
67
+ // 3. Solve (bundled HiGHS WebAssembly) and verify the solution
68
+ const status = await m.solve();
69
+ if (m.hasPrimalSolution) {
70
+ const report = m.verify();
71
+ console.log(status, report.valid); // "Optimal", true
72
+ console.log(`Profit: ${m.objectiveValue} (desks=${desks.value}, chairs=${chairs.value})`); // 1100 (desks=20, chairs=10)
73
+ }
74
+ ```
75
75
 
76
- 1. **Set realistic finite bounds (`lb`, `ub`)**: When defining variables with `m.continuous`, `m.integer`, or `m.binary`, finite bounds strengthen MIP presolve and enable automatic tight Big-M derivation for logical constraints.
77
- 2. **Formulate objectives and constraints with `expr`**: Use `` m.objective(maximize(expr`...`)) `` and `` m.constraint(expr`... <= ...`, "name") ``. Naming constraints lets you pinpoint conflicting rules immediately if a model becomes infeasible.
78
- 3. **Check `hasPrimalSolution` and call `validateSolution()`**: After `await m.solve()`, check `m.hasPrimalSolution` before reading `.value` on variables, and call `m.validateSolution()` to independently verify that bounds, integrality, and constraints hold within tolerance.
76
+ - **Finite bounds**: Supply finite `lb` and `ub` bounds on `m.continuous()` and `m.integer()` variables whenever known; they tighten MIP formulations and are required by `m.indicator()` when `M` is omitted.
77
+ - **Explicit registration & naming**: Constructing an expression with `` expr`...` `` does not attach it to the model until you call `m.objective()` or `m.constraint()`. Passing descriptive constraint names makes `m.check()` conflict reports readable.
78
+ - **Safe reading & verification**: Check `m.hasPrimalSolution` before reading `.value`, then call `m.verify()` to independently confirm bounds, integrality, and constraints.
79
79
 
80
80
  ---
81
81
 
82
82
  ## 🧭 Next Steps
83
83
 
84
- - **[Modeling Guide](../guide/variables.md)**: Array/dictionary variables, aggregations (`sum`, `dot`), logical constraints, and Unsat Core diagnosis
85
- - **[Examples & API Finder](../examples/)**: 11 runnable optimization and SMT recipes—from shift scheduling and portfolio QP to vehicle routing
86
- - **[Solvers Overview](../solvers/)**: Compare built-in HiGHS with optional Z3 and GLPK adapters
87
- - **[Runtime Environments](./runtimes.md)**: Setup details for Node.js, Vite / Next.js bundlers, CDNs, and static hosting
84
+ ### Choose Your Workflow
85
+
86
+ - **Build with an AI Coding Agent**: [Coding with AI Agents](./coding-agents.md) — Skill setup via `npx skills add ohtaman/washi`, `llms.txt` / Context7, and ready-to-use prompt templates.
87
+ - **Write Models in TypeScript**: [Modeling Guide](../guide/variables.md) — Variable containers, `sum` and `dot` aggregations, logical constraints, and conflict diagnosis.
88
+
89
+ ### Reference & Examples
90
+
91
+ - **[Examples & API Finder](../examples/)**: 12 runnable optimization, CP, and SMT recipes—from shift scheduling to portfolio QP.
92
+ - **[Solvers Overview](../solvers/)**: Side-by-side comparison of bundled HiGHS and Pumpkin against optional Z3 and GLPK.
93
+ - **[Runtime Environments](./runtimes.md)**: Setup details for Node.js, Vite/Next.js, CDNs, standalone HTML, and desktop/mobile WebViews.
@@ -1,6 +1,14 @@
1
1
  # Runtime Environments & Deployment
2
2
 
3
- WaSHI runs across JavaScript and TypeScript runtimes—from Node.js, Bun, and Deno to modern bundlers, static hosting, and vanilla browser `<script>` tags—without native C/C++ compilation.
3
+ WaSHI runs across JavaScript and TypeScript runtimes and deployment targets without native solver compilation. Supported targets include Node.js, Bun, Deno, modern bundlers, static hosting, classic `<script>` tags, and offline WebView apps.
4
+
5
+ | Target Environment | Entry Point | Worker & Header Notes |
6
+ | --- | --- | --- |
7
+ | **Node.js (v18+), Bun, Deno** | `@ohtaman/washi` (`npm:@ohtaman/washi` in Deno) | Loads bundled `.wasm` from disk; no HTTP headers required. |
8
+ | **Vite, Next.js, Webpack, Rollup** | `@ohtaman/washi` | Runs HiGHS and Pumpkin in Web Workers by default. |
9
+ | **Tauri, Capacitor, PWA** | `@ohtaman/washi` (standard Vite build) | Runs offline; allow `'wasm-unsafe-eval'` in `script-src` and `worker-src 'self' blob:` (or pass `{ useWorker: false }`) in your Content Security Policy (`CSP`). |
10
+ | **Browser ESM (`<script type="module">`)** | `dist/browser/washi.js` | Keep all `dist/browser/` files together when self-hosting. |
11
+ | **Standalone HTML / CDN (`<script>`)** | `dist/browser/washi.min.js` | Exposes `window.Washi`; CDN builds also run directly on `file://`. |
4
12
 
5
13
  ---
6
14
 
@@ -8,7 +16,7 @@ WaSHI runs across JavaScript and TypeScript runtimes—from Node.js, Bun, and De
8
16
 
9
17
  ### Node.js
10
18
 
11
- WaSHI supports Node.js (v18+) out of the box with standard ECMAScript Modules (ESM). The built-in HiGHS WebAssembly binary is loaded directly from the installed package:
19
+ WaSHI supports Node.js 18+ with standard ECMAScript Modules (`ESM`). Both bundled WebAssembly engines—HiGHS (default) and Pumpkin (`{ solver: "pumpkin" }`)—load directly from `solvers/` inside the installed package with zero extra imports:
12
20
 
13
21
  ```typescript
14
22
  import { Model, expr, maximize } from "@ohtaman/washi";
@@ -27,7 +35,7 @@ if (m.hasPrimalSolution) {
27
35
  }
28
36
  ```
29
37
 
30
- Run directly with `tsx` or `node`:
38
+ Run TypeScript files directly with `tsx` (or compiled `.js` with `node`):
31
39
 
32
40
  ```bash
33
41
  npx tsx app.ts
@@ -61,10 +69,10 @@ if (m.hasPrimalSolution) {
61
69
 
62
70
  ### Modern Bundlers & Static Deployment
63
71
 
64
- When importing `@ohtaman/washi` in a browser bundler, the `browser` export condition resolves automatically:
72
+ The package root export (`@ohtaman/washi`) resolves `import` and `default` to `./dist/lib/washi.js` (and `types` to `./dist/types/index.d.ts`). That unified ESM bundle detects Node.js versus browser runtimes automatically at execution time:
65
73
 
66
74
  ```typescript
67
- import { Model, expr, maximize } from "@ohtaman/washi";
75
+ import { Model, maximize } from "@ohtaman/washi";
68
76
 
69
77
  export async function runOptimization() {
70
78
  const m = new Model({ solver: "highs" });
@@ -75,35 +83,42 @@ export async function runOptimization() {
75
83
  }
76
84
  ```
77
85
 
78
- Because both the modeler and solver run on WebAssembly, the static output of `vite build` (or your bundler's production build) can be deployed directly to **GitHub Pages, Cloudflare Pages, Vercel, or Netlify** with no backend server required. (The default HiGHS and GLPK solvers require no special HTTP headers; if you use Z3 in the browser, see [Cross-Origin Isolation](./cross-origin.md).)
86
+ Because the modeler (JS/TS) and bundled solvers (WebAssembly) execute entirely on the client, static builds from `vite build` deploy directly to **GitHub Pages, Cloudflare Pages, Vercel, or Netlify** without a backend server. HiGHS, Pumpkin, and GLPK need no special HTTP headers; for Z3's browser headers, see **Offline, Intranet & Strict CSP Environments** below.
79
87
 
80
88
  #### Background Web Worker Execution (Default)
81
- In browsers, HiGHS automatically runs inside a dedicated **Web Worker** so LP/MIP/QP solves never freeze the main UI thread, animations, or user input, while enabling real-time progress events (`onProgress`) and cancellation (`AbortSignal`).
82
89
 
83
- If your environment prohibits Web Workers or you explicitly want main-thread execution, pass `useWorker: false`:
90
+ In browsers, **HiGHS** and **Pumpkin** run inside dedicated Web Workers by default. This keeps the main UI thread responsive during `LP`, `MIP`, `QP`, and `CP` solves while enabling `onProgress` callbacks and `AbortSignal` cancellation.
91
+
92
+ Pass `useWorker: false` if your environment prohibits Web Workers or you prefer main-thread execution:
84
93
 
85
94
  ```typescript
86
95
  await m.solve({ useWorker: false });
87
96
  ```
88
97
 
89
- #### Customizing the HiGHS WASM Asset URL
90
- By default, WaSHI resolves `highs.wasm` relative to the bundle (`new URL("./highs.wasm", import.meta.url)`). It **never** makes implicit requests to external CDNs (`jsdelivr`, `unpkg`, etc.).
98
+ #### Customizing WASM Asset URLs
99
+
100
+ By default, WaSHI resolves `solvers/highs.wasm` and `solvers/pumpkin.wasm` relative to the bundle via `import.meta.url`. It **never** falls back to external CDNs (`jsDelivr`, `unpkg`, etc.).
91
101
 
92
- If your bundler or static server hosts `.wasm` files under a custom path, configure the URL globally or per solve:
102
+ If your browser host serves `.wasm` files from a custom path, override the URL globally or per call. For HiGHS, `setHighsWasmUrl()` (from `@ohtaman/washi/solvers/highs`) and `{ wasmUrl }` are browser-only (ignored in Node.js); Pumpkin (`setPumpkinWasmUrl()` from `@ohtaman/washi/solvers/pumpkin`) also accepts `file://` URLs in Node.js, and Z3 uses `setZ3Assets()` or `{ z3JsUrl, z3WasmUrl }`:
93
103
 
94
104
  ```typescript
95
- import { setHighsWasmUrl } from "@ohtaman/washi";
105
+ import { setHighsWasmUrl } from "@ohtaman/washi/solvers/highs";
106
+ import { setPumpkinWasmUrl } from "@ohtaman/washi/solvers/pumpkin";
107
+ import { setZ3Assets } from "@ohtaman/washi/solvers/z3"; // Optional Z3 plugin
96
108
 
97
109
  // Option A: Configure globally before solving
98
110
  setHighsWasmUrl("/assets/wasm/highs.wasm");
111
+ setPumpkinWasmUrl("/assets/wasm/pumpkin.wasm");
112
+ setZ3Assets({ jsUrl: "/assets/z3/z3-built.js", wasmUrl: "/assets/z3/z3-built.wasm" });
99
113
 
100
- // Option B: Configure per solve call
114
+ // Option B: Configure per solve / check call
101
115
  await m.solve({ wasmUrl: "/assets/wasm/highs.wasm" });
116
+ // For Z3: await m.check({ solver: "z3", z3JsUrl: "...", z3WasmUrl: "..." });
102
117
  ```
103
118
 
104
119
  ### Next.js & Server-Side Rendering (SSR)
105
120
 
106
- In Next.js App Router or Pages Router, optimization models can run either in Server Actions / API Routes (Node.js runtime) or on the client inside `"use client"` components:
121
+ In Next.js App Router or Pages Router, models run either on the server (Server Actions / API Routes) or in the browser inside `"use client"` components. Below is a client component example:
107
122
 
108
123
  ```tsx
109
124
  "use client";
@@ -133,19 +148,37 @@ export default function OptimizerComponent() {
133
148
  }
134
149
  ```
135
150
 
151
+ ### Desktop & Mobile WebView Apps (Tauri / Capacitor / PWA)
152
+
153
+ Standard Vite frontends bundle **HiGHS** and **Pumpkin** as static assets for offline execution. They run in Tauri (`tauri://localhost` or `http://tauri.localhost`), Capacitor mobile WebViews, and offline PWAs with no native solver build step.
154
+
155
+ In `tauri.conf.json`, include `'wasm-unsafe-eval'` and `worker-src 'self' blob:` in your `CSP` string. If you run on the main thread with `{ useWorker: false }`, you can omit `worker-src` (see the full CSP table below):
156
+
157
+ ```json
158
+ {
159
+ "app": {
160
+ "security": {
161
+ "csp": "default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:"
162
+ }
163
+ }
164
+ }
165
+ ```
166
+
167
+ Prefer bundled HiGHS and Pumpkin in WebViews to avoid Z3's [Cross-Origin Isolation](./cross-origin.md) requirements.
168
+
136
169
  ---
137
170
 
138
171
  ## 📦 Browser ESM (`<script type="module">`)
139
172
 
140
- Without a bundler, you can import the pre-built browser ESM module directly from your own static server or an npm CDN:
173
+ Without a bundler, import the pre-built browser ESM module from your own static server or an npm CDN:
141
174
 
142
175
  ```html
143
176
  <script type="module">
144
- // From an npm CDN (or "./dist/browser/washi.js" when self-hosted):
145
- import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.6/dist/browser/washi.js";
177
+ // From an npm CDN (or "./dist/browser/washi.js" when self-hosted; includes HiGHS and Pumpkin):
178
+ import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.js";
146
179
  // Optional solver plugins:
147
- // import "https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.6/dist/browser/solvers/glpk.js";
148
- // import "https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.6/dist/browser/solvers/z3.js";
180
+ // import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/glpk.js";
181
+ // import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/z3.js";
149
182
 
150
183
  const m = new Model("BrowserESM");
151
184
  const x = m.integer("x", { lb: 0, ub: 100 });
@@ -166,7 +199,7 @@ Without a bundler, you can import the pre-built browser ESM module directly from
166
199
 
167
200
  ## 🟨 Classic `<script>` Tags (IIFE / Standalone HTML / CDN)
168
201
 
169
- For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a standard `<script>` tag. All exports are attached to the global `window.Washi` object:
202
+ For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a classic `<script>` tag. This immediately invoked function expression (`IIFE`) bundle exposes the core API on the global `window.Washi` object:
170
203
 
171
204
  ```html
172
205
  <!DOCTYPE html>
@@ -179,11 +212,11 @@ For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a stan
179
212
  <h1>WaSHI Browser Optimization</h1>
180
213
  <pre id="output">Solving...</pre>
181
214
 
182
- <!-- Core WaSHI + HiGHS (MIT) via CDN (or "./dist/browser/washi.min.js" when self-hosted) -->
183
- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.6/dist/browser/washi.min.js"></script>
184
- <!-- Optional plugins (auto-register on window.Washi): -->
185
- <!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.6/dist/browser/solvers/glpk.min.js"></script> -->
186
- <!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.6/dist/browser/solvers/z3.min.js"></script> -->
215
+ <!-- Core WaSHI + bundled HiGHS & Pumpkin (MIT) via CDN (or "./dist/browser/washi.min.js" when self-hosted) -->
216
+ <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js"></script>
217
+ <!-- Optional plugins (load after washi.min.js; auto-register when window.Washi is present): -->
218
+ <!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/glpk.min.js"></script> -->
219
+ <!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/z3.min.js"></script> -->
187
220
 
188
221
  <script>
189
222
  (async () => {
@@ -210,23 +243,44 @@ For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a stan
210
243
  </html>
211
244
  ```
212
245
 
213
- > [!NOTE]
214
- > When loading from a cross-origin CDN (such as jsDelivr or unpkg), browsers block direct `new Worker("https://cdn...")` construction under the same-origin policy. On standard web origins (`https://` / `http://`), WaSHI automatically bridges cross-origin workers via a lightweight ES module `Blob` bootstrap (`import "https://..."`). On opaque effective origins (`window.origin === "null"`, such as local `file:` URLs or `sandbox="allow-scripts"` `<iframe>` embeds without `allow-same-origin`), WaSHI automatically runs HiGHS on the main thread when `useWorker` is omitted.
246
+ ### Cross-Origin CDN Workers
247
+
248
+ On standard `http://` and `https://` origins, browsers block direct `new Worker("https://cdn...")` calls. WaSHI automatically bridges cross-origin CDN workers with a small ES module `Blob` bootstrap.
249
+
250
+ ### Opening Local `.html` Files Directly (`file://`)
251
+
252
+ | Scenario | Behavior & Requirement |
253
+ | --- | --- |
254
+ | **CDN scripts** | Loading `washi.min.js` from an HTTPS CDN includes both HiGHS and Pumpkin and works by double-clicking a local `.html` file (`file://`) with no local server. |
255
+ | **Opaque origins (`window.origin === "null"`)** | On `file://` pages or sandboxed `<iframe>` embeds without `allow-same-origin`, WaSHI runs HiGHS and Pumpkin on the main thread when `useWorker` is omitted. |
256
+ | **Self-hosted `.wasm` & Z3** | Local `.wasm` files require `http://` or `https://` because browsers block `fetch()` on `file://`. Z3 also requires Cross-Origin Isolation (see below). |
215
257
 
216
258
  ---
217
259
 
218
260
  ## 🔒 Offline, Intranet & Strict CSP Environments
219
261
 
220
- WaSHI never phones home or falls back to public CDNs. To self-host in an air-gapped intranet or under a strict Content Security Policy (CSP):
221
-
222
- 1. **Copy Static Assets**: Serve the files from `node_modules/@ohtaman/washi/dist/browser/` on your own origin:
223
- - `washi.js` / `washi.min.js`, `highs.wasm`, and `highs-worker.js`
224
- - Optional GLPK: `solvers/glpk.js` / `solvers/glpk.min.js`
225
- - Optional Z3: `solvers/z3.js` / `solvers/z3.min.js`, `solvers/z3-built.js`, `solvers/z3-built.wasm`
226
- 2. **Content Security Policy (CSP) Headers**:
227
- - Allow WebAssembly compilation (`'wasm-unsafe-eval'` in `script-src`).
228
- - If using the default Web Worker or cross-origin Blob worker bootstrap, allow `worker-src 'self' blob:;`.
229
- - If `blob:` workers are prohibited by policy, either serve `highs-worker.js` from `'self'` or run on the main thread with `await m.solve({ useWorker: false })`.
230
- 3. **Cross-Origin Isolation (Z3 Only)**:
231
- - HiGHS and GLPK run in standard browser contexts without special headers.
232
- - Z3 WASM uses pthreads + `SharedArrayBuffer` and requires `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp` (`credentialless` works in Chromium/Firefox, whereas Safari requires `require-corp`). See the [Cross-Origin Isolation Guide](./cross-origin.md) for setup details.
262
+ WaSHI never phones home or falls back to public CDNs. To self-host in an air-gapped intranet or under a strict `CSP`, configure three areas:
263
+
264
+ ### 1. Deploy `dist/browser/` Intact
265
+
266
+ Copy `node_modules/@ohtaman/washi/dist/browser/` to your static origin as a complete directory. Even the classic IIFE bundle (`washi.min.js`) spawns `solvers/highs-worker.js` and `solvers/pumpkin-worker.js` as Web Workers by default unless `{ useWorker: false }` is passed. Both the ESM entry points and these worker scripts import hashed `*.js` chunks under `dist/browser/` via relative paths.
267
+
268
+ | Bundle | Required Files under `dist/browser/` |
269
+ | --- | --- |
270
+ | **Core** | `washi.js` / `washi.min.js` |
271
+ | **Bundled HiGHS** | `solvers/highs.js`, `solvers/highs.wasm`, `solvers/highs-worker.js`, and `solvers/highs-*.js` chunks |
272
+ | **Bundled Pumpkin** | `solvers/pumpkin.js`, `solvers/pumpkin.wasm`, `solvers/pumpkin-worker.js`, and `solvers/pumpkin-*.js` chunks |
273
+ | **Optional GLPK** | `solvers/glpk.js` / `solvers/glpk.min.js` |
274
+ | **Optional Z3** | `solvers/z3.js` / `solvers/z3.min.js`, `solvers/z3-built.js`, `solvers/z3-built.wasm` |
275
+
276
+ ### 2. Configure CSP Directives
277
+
278
+ | Directive | Required Value | Purpose |
279
+ | --- | --- | --- |
280
+ | `script-src` | `'wasm-unsafe-eval'` | Allows WebAssembly module compilation. |
281
+ | `worker-src` | `'self' blob:` | Allows default Web Workers and CDN `Blob` worker bootstraps. |
282
+ | `worker-src` (if `blob:` is disallowed) | `'self'` only | Serve `solvers/highs-worker.js` / `solvers/pumpkin-worker.js` from `'self'`, or pass `{ useWorker: false }`. |
283
+
284
+ ### 3. Enable Cross-Origin Isolation (Z3 Only)
285
+
286
+ HiGHS, Pumpkin, and GLPK run in standard browser contexts without isolation headers. Z3 uses `SharedArrayBuffer` multithreading, so browsers require a **Secure Context** (`https://` or `http://localhost`). You must also serve `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy` headers (`COOP`/`COEP`); see the [Cross-Origin Isolation Guide](./cross-origin.md).