npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@carpediemnn/node-red-ai-gateway

v0.1.0

Published

Node-RED AI gateway nodes for OpenAI-compatible APIs with hidden credentials and prompt-cache observability.

Readme

@carpediemnn/node-red-ai-gateway

一个面向 OpenAI-compatible 网关的 Node-RED AI 节点包。核心目标是:

  • API Key 不暴露在 flow JSON、节点输出和日志里。
  • 把常见 AI 能力收敛到一个主节点里,画布更干净。
  • 请求结构对 provider-side prompt cache 更友好。
  • cached_tokens、token usage、prefix hash 等诊断信息输出到 msg.ai

安装

在 Node-RED 编辑器中打开:

菜单 -> Manage palette -> Install

搜索并安装:

@carpediemnn/node-red-ai-gateway

也可以在 Node-RED 用户目录中安装:

cd ~/.node-red
npm install @carpediemnn/node-red-ai-gateway

Docker 版 Node-RED 通常在容器的 /data 目录安装:

cd /data
npm install @carpediemnn/node-red-ai-gateway

安装后重启 Node-RED。

当前设计

推荐的新 flow 结构是:

AI Provider  ->  AI Gateway

AI Provider

配置节点,只负责连接和安全:

  • API Base,例如 https://api.openai.com/v1 或自己的网关 /v1 地址。
  • API Key,使用 Node-RED credentials 的 password 字段保存。
  • 鉴权方式:Bearer Token、自定义 Header 原始值、无鉴权。
  • 默认模型。
  • prompt_cache_key 开关。
  • prompt_cache_retention 设置。

API Key 不会作为普通节点配置保存,导出 flow 时也不会出现在 JSON 里。

AI Gateway

推荐主节点。一个节点里通过 Mode 选择能力:

  • Text / Chat:普通文本生成、总结、分析。
  • JSON:让模型输出 JSON,并解析成对象。
  • Classify:输出 { label, confidence, reason }
  • Router:先分类,再按 label 顺序发到对应 output。
  • Request:高级模式,直接请求任意 OpenAI-compatible endpoint。
  • Cache Probe:用同一个稳定前缀连续请求两次,检查真实 provider cache 是否命中。

当前包只暴露这两个节点:AI ProviderAI Gateway

Prompt Cache 友好策略

节点会尽量把稳定内容放在请求前缀里:

  1. System prompt
  2. Few-shot examples
  3. JSON schema / labels / routing rules
  4. 动态输入,例如 msg.payload

这样相同任务的前缀更稳定,更容易命中 AI 服务商侧 prompt cache。

当 Provider 开启 prompt_cache_key 时,节点会根据稳定前缀、模型、API Base 生成稳定 key。

运行时输出

AI Gateway 会把诊断信息写到 msg.ai

msg.ai = {
  provider: "openai-compatible",
  model: "openai/gpt-5.4-mini",
  endpoint: "/chat/completions",
  cache: {
    providerCachedTokens: 8448,
    promptTokens: 9036,
    cacheHitRate: 0.9349,
    prefixHash: "69298b89cea6bbe2"
  },
  usage: {
    promptTokens: 9036,
    completionTokens: 5,
    totalTokens: 9041,
    raw: {}
  }
}

如果是 AI GatewayCache Probe 模式,输出会包含:

msg.payload = {
  prefixHash: "...",
  warmed: true,
  first: {
    cache: { providerCachedTokens: 0 }
  },
  second: {
    cache: { providerCachedTokens: 8448 }
  }
}

安全设计

  • API Key 存在 AI Provider 的 credentials password 字段里。
  • 运行时服务端代码负责组装鉴权 Header。
  • msg 输出不包含 API Key。
  • 错误信息和请求摘要会脱敏。
  • 导出 flow JSON 不包含 API Key。

示例

仓库内置了一个基础示例 flow:

examples/gateway-basic-flow.json

导入后,打开 AI Provider,填写自己的 API Base、模型和 API Key 即可运行。

本地开发

npm test
npm run validate

安装到本地 Node-RED:

cd ~/.node-red
npm install /Users/shiyanan/Documents/nodered-ai-node

发布前检查:

npm run validate
npm publish --dry-run

当前测试覆盖:

  • 节点注册。
  • API Key credentials 声明。
  • AI Gateway 的 Text、JSON、Request、Cache Probe、Router 模式。
  • prompt 前缀稳定性。
  • cached token 解析。
  • 错误脱敏。