add-middleware

작성자: nvidia

Add a new guardrail or intercept type to the NeMo Relay middleware pipeline

npx skills add https://github.com/nvidia/nemo-relay --skill add-middleware

Add a Middleware Type

Companion Guidance

Use karpathy-guidelines alongside this skill for implementation or review work. Keep changes scoped, surface assumptions, and define focused validation before editing.

NeMo Relay supports guardrails (validate/gate) and intercepts (transform) at various pipeline stages. Adding a new middleware type requires changes across all layers.

Use this skill when introducing a new middleware registration surface or adding middleware behavior to a new pipeline stage.

Lock The Design First

Decide these before editing code:

  • Is this for tools, LLMs, or both?
  • Is it a conditional guardrail, sanitize guardrail, request intercept, or execution intercept?
  • Does it run on request input, inner callable execution, stream chunks, or final response output?
  • Is the callback fallible, and how should callback failures propagate?
  • Does it need both global and scope-local registration?
  • What should subscribers observe in event.input and event.output after this middleware runs?

Pipeline Order

See docs/about/concepts/middleware.md for the full diagrams.

  • Tool execute: conditional guardrails -> request intercepts -> sanitize request (for events) | execution intercept chain(callable) -> sanitize response
  • LLM execute: conditional guardrails -> request intercepts -> sanitize request (for events) | execution intercept chain(callable) -> sanitize response

Core Steps

  1. Define or reuse the callback type alias in crates/core/src/api/runtime/callbacks.rs.
pub type MyNewFn = Box<dyn Fn(&str, Json) -> Json + Send + Sync>;
  1. Add the registry field to NemoRelayContextState in crates/core/src/api/runtime/state.rs.

Add a SortedRegistry<GuardrailEntry<MyNewFn>> or SortedRegistry<Intercept<MyNewFn>> field to the state struct.

  1. Add registration and deregistration APIs in crates/core/src/api/.

Use the existing global_*_registry_api! and scope_*_registry_api! macro patterns in crates/core/src/api/registry.rs. Both global and scope-local variants are needed unless the design explicitly rules one out.

  1. Add chain execution helpers to NemoRelayContextState in crates/core/src/api/runtime/state.rs.

Follow the pattern of tool_sanitize_request_chain or tool_request_intercepts_chain.

  1. Wire the chain into the execute path.

Update crates/core/src/api/tool.rs or crates/core/src/api/llm.rs to call the new chain method at the appropriate pipeline stage.

  1. Expose the new middleware surface in every affected binding.

Follow the add-binding-feature skill for the cross-binding implementation checklist.

Required Tests

  • Registration and duplicate-name behavior
  • Deregistration and no-op missing-name behavior
  • Ordering by priority
  • Callback error propagation
  • Scope-local registration, inheritance, and cleanup on pop
  • Event input/output semantics after middleware mutation
  • Parity coverage in every affected binding

Key References

  • Pipeline logic: crates/core/src/api/tool.rs, crates/core/src/api/llm.rs
  • Type aliases: crates/core/src/api/runtime/callbacks.rs
  • Runtime state and chain builders: crates/core/src/api/runtime/state.rs
  • Scope-local registry merging: crates/core/src/context/registries.rs
  • Registry: crates/core/src/registry.rs
  • Pipeline docs: docs/about/concepts/middleware.md
  • Architecture docs: docs/about/architecture.md
  • Registration examples: docs/instrument-applications/advanced-guide.md
  • Validation: validate-change

nvidia의 다른 스킬

compileiq-debug
nvidia
Use when something is wrong: Search() hangs, all evaluations return INVALID_SCORE, scores aren't improving, every config returns the same number, ptxas errors…
official
create-github-pr
nvidia
gh CLI를 사용하여 GitHub 풀 리퀘스트를 생성합니다. 사용자가 새 PR을 만들거나, 코드 리뷰를 제출하거나, 풀 리퀘스트를 열고자 할 때 사용합니다. 트리거 키워드 -…
official
diagnose-perf
nvidia
First-responder performance triage for Isaac Sim and Isaac Lab. Identifies bottleneck category (GPU-bound, CPU-bound, VRAM, loading) using nvidia-smi and…
official
eagle3-review-logs
nvidia
Review EAGLE3 pipeline experiment logs from the launcher's experiments/ directory. Summarizes pass/fail status for all 4 tasks, diagnoses failures with root…
official
nemoclaw-maintainer-cross-issue-sweep
nvidia
다른 열린 이슈들을 스캔하여 주어진 PR이 함께 수정하거나 실수로 망가뜨릴 수 있는 이슈를 찾습니다. 인접 수정 기회와 모순 위험을 file:line…과 함께 출력합니다.
official
karpathy-guidelines
nvidia
일반적인 LLM 코딩 실수를 줄이기 위한 행동 지침입니다. 코드 작성, 검토 또는 리팩토링 시 과도한 복잡성을 피하고 정밀한 변경을 위해 사용하세요.
official
fhir-basics
nvidia
에이전트에게 FHIR R4 API의 작동 방식, 사용 가능한 리소스, 검색 매개변수를 사용한 쿼리 방법, 모든 응답 형식을 올바르게 파싱하는 방법을 가르칩니다…
official
underdeclared-agent
nvidia
A helpful assistant agent
official