webnn
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.
More from webmaxru/agent-skills
github-agentic-workflows
Authors, reviews, installs, and debugs GitHub Agentic Workflows in repositories, including workflow markdown, frontmatter, gh aw compile and run flows, safe outputs, security guardrails, and operational patterns. Use when creating or maintaining GH-AW automation. Don't use for standard deterministic GitHub Actions YAML, generic CI pipelines, or non-GitHub automation systems.
96proofreader-api
Implements and debugs browser Proofreader API integrations in JavaScript or TypeScript web apps. Use when adding Proofreader availability checks, monitored model downloads, proofread flows, correction metadata handling, or permissions-policy checks for built-in proofreading. Don't use for generic prompt engineering, server-side LLM SDKs, or cloud AI services.
93webmcp
Implements and debugs browser WebMCP integrations in JavaScript or TypeScript web apps. Use when exposing imperative tools through navigator.modelContext, annotating HTML forms for declarative tools, handling agent-invoked form flows, or validating WebMCP behavior in the current Chrome preview. Don't use for server-side MCP servers, REST tool backends, or non-browser providers.
92prompt-api
Implements and debugs browser Prompt API integrations in JavaScript or TypeScript web apps. Use when adding LanguageModel availability checks, session creation, prompt or promptStreaming flows, structured output, download progress UX, or iframe permission-policy handling. Don't use for server-side LLM SDKs, REST AI APIs, or non-browser providers.
92writing-assistance-apis
Implements and debugs browser Summarizer, Writer, and Rewriter integrations in JavaScript or TypeScript web apps. Use when adding availability checks, model download UX, session creation, summarize or write or rewrite flows, streaming output, abort handling, or permissions-policy constraints for built-in writing assistance APIs. Don't use for generic prompt engineering, server-side LLM SDKs, or cloud AI services.
92agent-package-manager
Installs, configures, audits, and operates Agent Package Manager (APM) in repositories. Use when initializing apm.yml, installing or updating packages, validating manifests, managing lockfiles, compiling agent context, browsing MCP servers, setting up runtimes, or packaging resolved context for CI and team distribution. Don't use for writing a single skill by hand, generic package managers like npm or pip, or non-APM agent configuration systems.
92