feat: add Java integration via Chicory WebAssembly runtime#561
feat: add Java integration via Chicory WebAssembly runtime#561
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
mise.toml
Outdated
| dir = "cli" | ||
| run = """ | ||
| if [ ! -d "binaryen" ]; then | ||
| wget https://github.com/WebAssembly/binaryen/releases/download/version_125/binaryen-version_125-x86_64-linux.tar.gz |
There was a problem hiding this comment.
this should be enough in CI, we can expand on OS/arch compatibility as a follow up
Greptile SummaryThis PR adds Java integration for the Key highlights:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client as Java Client
participant Builder as Usage.Builder
participant Exec as Usage.exec()
participant Chicory as Chicory WASI Runtime
participant WASM as usage.wasm (Rust binary)
Client->>Builder: Usage.builder()<br/>.withStdin(spec)<br/>.withArgs("generate","json","-f","-")
Builder->>Exec: run() → exec(stdin, args, directory)
Exec->>Exec: Create ByteArrayOutputStream (stdout, stderr)
Exec->>Chicory: WasiOptions.builder()<br/>.withStdin()<br/>.withStdout()<br/>.withStderr()<br/>.withArguments()
Note over Exec,Chicory: Optional: .withDirectory(path) for file output
Exec->>Chicory: WasiPreview1.builder().withOptions().build()
Exec->>Chicory: Instance.builder(MODULE)<br/>.withMachineFactory(UsageModule::create)<br/>.withImportValues(wasi.toHostFunctions())<br/>.build()
Chicory->>WASM: Execute _start (AOT-compiled)
WASM-->>Chicory: writes to stdout/stderr via WASI
WASM->>Chicory: proc_exit(code)
Chicory-->>Exec: throws WasiExitException(exitCode)
Exec->>Exec: wasi.close() (try-with-resources)
Exec-->>Builder: new UsageResult(stdout, stderr, exitCode)
Builder-->>Client: UsageResult
|
- Make cli and lib compile for wasm32-wasip1: gate xx dependency behind cfg(not(wasm32)), replace xx::file/xx::regex with std::fs and regex::Regex + LazyLock, add wasm32 stubs for shell execution - Add [profile.wasm] Cargo profile (opt-level=z, strip=symbols) for smaller WASM output (4.5M vs 6.2M default) - Migrate cli/Makefile targets to mise wasm:* tasks - Add integrations/java/ with Usage wrapper that runs the usage CLI compiled to WASM via Chicory with build-time AOT compilation - Add CI workflow for Java integration (mvn test + jbang example) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
- Fix wasm:clean to remove workspace-root target/wasm32-wasip1 instead of cli/target which doesn't exist in a Cargo workspace - Remove unused wasm:get-binaryen task Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #561 +/- ##
==========================================
- Coverage 61.98% 58.75% -3.24%
==========================================
Files 48 48
Lines 7637 7777 +140
Branches 7637 7777 +140
==========================================
- Hits 4734 4569 -165
- Misses 1543 1671 +128
- Partials 1360 1537 +177 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I'll fix the CI failure tomorrow, sorry for the noise. |
The previous SHA didn't exist. Pin to v0.1.1. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Pin maven-surefire-plugin to 3.5.2 for reliable JUnit 5 discovery - Return defensive copies from UsageResult.stdout()/stderr() - Catch RuntimeException (WASM traps) and surface as exit code 1 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
I think I addressed all the comments but: } catch (RuntimeException e) {
// WASM trap or other Chicory error — surface as non-zero exit
return new UsageResult(stdout.toByteArray(), stderr.toByteArray(), 1);
}I disagree we should catch |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@jdx is this interesting to you or outside the scope of the project? Would you be up in accepting a minimal set of changes to enable the compilation to Wasm without having to patch sources? Thanks in advance 🙏 |
|
I wouldn't accept this as is. I'm not sure what a minimal patch would look like but possibly. wasm definitely isn't a priority for me |
|
Thanks for getting back! I put up #570 for the minimal patch for you to evaluate the impact 🙏 Closing this one. |
Thanks for the great project! @maxandersen asked me to look into it to see if it can be easily consumed from a Java application and here we are 🙂
Here I added support for running the
usageCLI natively in Java as a plain dependency, powered by Chicory (a pure-Java WebAssembly runtime with zero native dependencies).Changes
xxdependency behindcfg(not(wasm32)), replacexx::file/xx::regexwithstd::fsandregex::Regex+LazyLock, add wasm32 stubs for shell execution[profile.wasm]inCargo.toml— dedicated Cargo profile (opt-level = "z",strip = "symbols") for smaller WASM output (4.5M vs 6.2M default)mise.tomlwasm tasks — migratedcli/Makefiletargets to mise (wasm:build,wasm:get-wasisdk,wasm:get-binaryen,wasm:clean)integrations/java/— Java wrapper that runsusagecompiled to WASM via Chicory with build-time AOT compilation.github/workflows/test-java.yml— CI workflow for the Java integration (mvn test + jbang example)Java API
Next steps
To move this forward I'd be happy to discuss if you have any interest in publishing the Java package (super minimal dependencies) or I can set up a
usage4jrepository under the roastedroot organization. Either way, the Rust changes (wasm compat +[profile.wasm]+ mise tasks) would be nice to have here regardless.