@framers/agentos-ext-topicality

中文文档
0.2.5发布于 2026/11/10Apache-2.0TypeScriptES Module零依赖近 30 天下载 2万

Topic enforcement guardrail for AgentOS — embedding-based on/off-topic detection with LLM fallback

原始使用文档
文档目录9 节
  1. What it does
  2. Install
  3. Quickstart
  4. With no topics, or one list
  5. Logging
  6. Public API
  7. Examples
  8. Lazy loading and optional install
  9. License

@framers/agentos-ext-topicality

Embedding-based on/off-topic detection guardrail for @framers/agentos, with optional LLM fallback for ambiguous cases.

What it does

Compares incoming user messages against a configured topic policy. It checks input only: agent output passes through. An input close to a blocked topic is blocked; an input close to none of the allowed topics is flagged as off-topic.

Each input goes to the first tier that answers:

  1. Embeddings. The guardrail imports @huggingface/transformers (an optional dependency), loads Xenova/all-MiniLM-L6-v2 once per process, and compares cosine similarity with each topic against maxBlockedSimilarity (default 0.5) and minSimilarity (default 0.3). When the package is missing or the model fails to load, each guardrail writes one warning with the error and the tier it uses instead (see Logging), and goes to the next tier.
  2. LLM judge. When you pass an llmInvoker callback, the guardrail asks the model for a JSON verdict. A failed call goes to the next tier.
  3. Keywords. A case-insensitive substring match between the input and each topic string, always available.

Use it to keep a customer-support agent on the product's domain.

Install

npm install @framers/agentos-ext-topicality

This package is an AgentOS extension pack. It expects @framers/agentos as a peer dependency.

Quickstart

import { AgentOS } from '@framers/agentos';
import { createTopicalityGuardrail } from '@framers/agentos-ext-topicality';

const agentos = new AgentOS();
await agentos.initialize({
  extensionManifest: {
    packs: [
      {
        factory: () =>
          createTopicalityGuardrail({
            allowedTopics: ['product support', 'billing', 'account access'],
            blockedTopics: ['politics'],
            minSimilarity: 0.3,
          }),
        enabled: true,
      },
    ],
  },
});

With no topics, or one list

createTopicalityGuardrail(); // or a manifest entry with no options
// first input: topicality: no allowedTopics or blockedTopics configured, so this guardrail passes every input

A pack given neither allowedTopics nor blockedTopics (a manifest entry with no options, or the extensions registry's options, which carry only a priority and secrets) passes every input. On its first input it writes the line above once, through the logger the host passes in the pack's context, else the activation context's, else the console.

One list alone enforces: blockedTopics alone blocks a matching input and lets the rest through; allowedTopics alone flags an input that matches none of them.

Logging

When the embedding tier cannot load, each guardrail writes one line:

topicality: the embedding tier is unavailable (Xenova/all-MiniLM-L6-v2 could not load: <error>); deciding with the keyword tier

Like the line about missing topics, it goes to the logger the host passes in the pack's context, else the activation context's, else the console.

Public API

  • TopicalityGuardrail — guardrail class implementing the AgentOS guardrail contract
  • CheckTopicTool — tool agents can invoke to ask the guardrail for an explicit decision
  • createTopicalityGuardrail(options) — factory returning an ExtensionPack
  • createExtensionPack(context) — auto-discoverable factory used by AgentOS extension auto-pickup
  • cosineSimilarity, clearEmbeddingCache — utilities for direct use

See src/types.ts for the full options shape (TopicalityOptions).

Examples

  • test/ — unit tests demonstrating threshold tuning and fallback behavior

Lazy loading and optional install

This package is an optional dependency of @framers/agentos-extensions-registry. The registry ships catalog metadata; createCuratedManifest() calls import.meta.resolve() per entry and silently skips anything not installed. npm install @framers/agentos-ext-topicality is the gate.

Nothing loads at activation. The guardrail imports @huggingface/transformers and loads the embedding model on the first input it checks. Embeddings are cached per text in memory; clearEmbeddingCache() empties the cache, and the pack calls it on deactivation.

The LLM tier runs only when you pass an llmInvoker.

The guardrail registers with config.canSanitize = false and config.evaluateStreamingChunks = false: it never rewrites text and runs in Phase 2 of the two-phase dispatcher (parallel classifiers). A blocked-topic match returns BLOCK; an off-topic input returns FLAG.

For the full DI model and end-to-end walkthrough, see How extensions stay optional and lazy and the auto-loading guide.

License

Apache 2.0 — see the repo root LICENSE.

版本与发布记录

@framers/agentos-ext-topicality 共发布 6 个版本,首次发布于 2026/03/22;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.2.5latest2026/11/105 天前
0.2.42026/11/097 天前
0.2.32026/10/2126 天前
0.2.22026/05/185 个月前
0.2.12026/03/257 个月前
0.1.02026/03/227 个月前

关于 @framers/agentos-ext-topicality 的常见问题

@framers/agentos-ext-topicality 是什么?

Topic enforcement guardrail for AgentOS — embedding-based on/off-topic detection with LLM fallback

如何安装 @framers/agentos-ext-topicality?

在项目目录运行 npm install @framers/agentos-ext-topicality,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@framers/agentos(>=0.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@framers/agentos-ext-topicality 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@framers/agentos-ext-topicality 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@framers/agentos-ext-topicality 可以商用吗?

@framers/agentos-ext-topicality 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@framers/agentos-ext-topicality 的下载量有多少?

根据 npm 官方下载统计,@framers/agentos-ext-topicality 在 2026/10/16 – 2026/11/14 这 30 天内被下载 20,277 次,日均约 676 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@framers/agentos-ext-topicality 还在维护吗?

最新版本 0.2.5 发布于 2026/11/10(5 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/03/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。