Implements and debugs browser Web Neural Network API integrations in JavaScript or TypeScript web apps. Use when adding navigator.ml checks, MLContext creation, MLGraphBuilder flows, device selection, tensor dispatch and readback, or explicit fallback paths to ONNX Runtime Web or other local runtimes. Don't use for model training, server-side ML inference, or cloud AI APIs.
Install
npx skills add https://github.com/webmaxru/web-ai-agent-skills --skill webnnSKILL.md
WebNN
Procedures
Step 1: Identify the browser integration surface
- Inspect the workspace for browser entry points, UI handlers, worker entry files, and any existing model-loading or inference abstraction layer.
- Execute
node scripts/find-webnn-targets.mjs .to inventory likely frontend files and existing WebNN markers when a Node runtime is available. - If a Node runtime is unavailable, inspect the nearest
package.json, HTML entry point, framework bootstrap files, and worker entry files manually to identify the browser app boundary. - If the workspace contains multiple frontend apps, prefer the app that contains the active route, component, or user-requested feature surface.
- If the inventory still leaves multiple plausible frontend targets, stop and ask which app should receive the WebNN integration.
- If the project is not a browser web app, stop and explain that this skill does not apply.
Step 2: Confirm WebNN viability and choose the runtime shape
- Read
references/webnn-reference.mdbefore writing code. - Read
references/examples.mdwhen choosing between a direct WebNN graph flow and an adapter around an existing browser ML runtime. - Read
references/compatibility.mdwhen native support, preview flags, device behavior, or backend differences matter. - Read
references/troubleshooting.mdwhen context creation, graph build, tensor readback, or device selection fails. - Verify that the feature runs in a secure context and in a
WindoworWorkercontext (DedicatedWorker,SharedWorker, orServiceWorker). - If the feature must run on the server, train models, or depend on cloud inference, stop and explain the platform mismatch.
- Choose device intent deliberately: use
powerPreference: "high-performance"for throughput,powerPreference: "low-power"for power-efficient acceleration, oraccelerated: falseto prefer CPU inference for maximum reach. - Treat
acceleratedandpowerPreferenceas preferences, not guarantees. Browser backends can still partition graphs or fall back per operator. - Choose a direct
MLGraphBuilderflow when the application owns graph construction or can keep a small deterministic graph path. - Choose an adapter around an existing local runtime only when the application already loads models through that runtime and the task is to prefer WebNN acceleration without rewriting the full inference stack.
- If the project uses TypeScript, add or preserve typings for the WebNN surface used by the project.
Step 3: Implement a guarded runtime adapter
- Read
assets/webnn-runtime.template.tsand adapt it to the framework, state model, and file layout in the workspace. - Centralize support detection around
window.isSecureContext,navigator.ml, and the requested execution context instead of scattering checks through UI components. - Create an
MLContextonly at the boundary where the app is ready to initialize local inference. - Pass explicit
acceleratedandpowerPreferencevalues when the product has a real preference, and omit tuning that the product cannot justify. - Build the graph through
MLGraphBuilderwhen the feature uses direct WebNN operations, or route existing model execution through the app's existing local runtime adapter when that runtime is already responsible for model loading and pre/post-processing. - Reuse the compiled graph and reusable tensors when input and output shapes stay stable across requests.
- Use
context.writeTensor(),context.dispatch(), andawait context.readTensor()in that order for direct graph execution. - Observe
context.lostand rebuild the context, graph, and tensors if the browser invalidates the execution state. - Destroy tensors, graphs, and contexts when the feature is disposed or the route no longer needs them.
Step 4: Wire UX and fallback behavior
- Surface distinct states for unsupported browsers, secure-context failures, runtime preparation, ready native execution, and explicit fallback execution.
- Keep a non-WebNN path for unsupported browsers or unsupported devices when the feature must remain available.
- Keep the fallback explicit and product-approved. Do not silently swap in a remote model provider when the feature is supposed to stay local.
- Present device choice as an intent, not a promise that every operator will execute on that device.
- Move long-running model preparation or repeated inference off the main thread when the application already uses a worker-friendly architecture.
- Keep all user data handling consistent with the product's local-processing promises and privacy requirements.
Step 5: Validate behavior
- Execute
node scripts/find-webnn-targets.mjs .to confirm that the intended app boundary and WebNN markers still resolve to the edited integration surface. - Verify secure-context and
navigator.mldetection before debugging deeper runtime issues. - For direct WebNN paths, run a smoke test that creates a context, builds a trivial graph, writes inputs, dispatches, and reads outputs.
- Test the intended
acceleratedandpowerPreferencesettings and confirm that fallback behavior remains usable when an accelerated context cannot be created. - Use
context.opSupportLimits()when operator coverage or tensor data type support influences graph design. - Confirm the app does not reuse destroyed tensors, graphs, or contexts.
- If the target environment depends on preview Chromium flags or milestone-specific behavior, confirm the required browser state from
references/compatibility.mdbefore treating runtime failures as application bugs. - Run the workspace build, typecheck, or tests after editing.
Error Handling
- If
navigator.mlis missing, confirm secure-context requirements and browser support fromreferences/compatibility.mdbefore changing application code. - If
createContext()fails for an accelerated or high-performance request, retry only through the product's approved fallback plan and surface the failure reason. - If
build()ordispatch()fails, checkreferences/examples.mdandreferences/troubleshooting.mdfor operator, shape, and device mismatches before rewriting the feature. - If
context.lostresolves, treat the current context, graph, and tensors as invalid and recreate them before the next inference attempt. - If the product only has a remote inference contract, stop and explain that this skill does not directly apply.
Related skills
repo-intake-and-planlllllllama450KRigor Intake helper for README-first deep learning repo reproduction. Use when the task is specifically to scan a repository, read the README and common project files, extract documented commands, classify inference, evaluation, and training candidates, and return the smallest trustworthy reproduction plan to the main orchestrator. Do not use for environment setup, asset download, command execution, final reporting, paper lookup, or end-to-end orchestration.minimal-run-and-auditlllllllama450KRigor Run skill for README-first deep learning repo reproduction. Use when the task is specifically to capture or normalize evidence from the selected smoke test or documented inference or evaluation command and write standardized `repro_outputs/` files, including patch notes when repository files changed. Do not use for training execution, initial repo intake, generic environment setup, paper lookup, target selection, hidden scientific-meaning changes, or end-to-end orchestration by itself.ai-research-reproductionlllllllama311KRigor Reproduce compatible skill slug for README-first deep learning repository reproduction. Use when the user wants an end-to-end, minimal-trustworthy flow that reads the repository first, selects the smallest documented inference or evaluation target, coordinates intake, setup, trusted execution, optional trusted training, optional repository analysis, and optional paper-gap resolution, enforces conservative patch rules, records evidence assumptions deviations and human decision points, and wexplore-codelllllllama311KRigor Improve implementation leaf skill for auditable candidate implementation in deep learning research repositories. Use when the researcher explicitly authorizes exploratory work on an isolated branch or worktree to transplant modules, adapt a backbone, add LoRA or adapter layers, replace a head, or stitch together meaningful low-risk migration ideas with rollback-aware records in `explore_outputs/`. Do not use for end-to-end exploration orchestration on top of `current_research`, trusted bas