open-classify 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,9 +4,11 @@ import { fileURLToPath } from "node:url";
4
4
  import { buildClassifierPrompt } from "./stock-prompt.js";
5
5
  import { validateJsonClassifierManifest, validateOutputForManifest, } from "./stock-validation.js";
6
6
  const __dirname = dirname(fileURLToPath(import.meta.url));
7
- const CLASSIFIERS_DIR = join(__dirname, "classifiers");
7
+ export const BUILTIN_CLASSIFIERS_DIR = join(__dirname, "classifiers");
8
8
  // Directories whose names start with "_" are reserved for shared assets
9
- // (e.g. `_prompts/`) and are not loaded as classifiers.
9
+ // (e.g. `_prompts/`) and are not loaded as classifiers. Consumers can use
10
+ // the same convention in their own classifier directories: rename a
11
+ // classifier to `_<name>/` to deactivate it without deleting it.
10
12
  const SHARED_DIRECTORY_PREFIX = "_";
11
13
  export class ClassifierManifestError extends Error {
12
14
  constructor(message) {
@@ -14,7 +16,10 @@ export class ClassifierManifestError extends Error {
14
16
  this.name = "ClassifierManifestError";
15
17
  }
16
18
  }
17
- export function loadClassifierRegistry(classifiersDir = CLASSIFIERS_DIR) {
19
+ // Load all classifier manifests under a single directory. Used internally to
20
+ // load the built-ins and each extra directory; callers wanting the merged
21
+ // registry should use `buildClassifierRegistry()` instead.
22
+ export function loadClassifierRegistry(classifiersDir = BUILTIN_CLASSIFIERS_DIR) {
18
23
  if (!existsSync(classifiersDir)) {
19
24
  throw new ClassifierManifestError(`classifier directory not found: ${classifiersDir}`);
20
25
  }
@@ -26,11 +31,29 @@ export function loadClassifierRegistry(classifiersDir = CLASSIFIERS_DIR) {
26
31
  continue;
27
32
  manifests.push(loadClassifierManifest(join(classifiersDir, entry.name)));
28
33
  }
29
- // Lower dispatch_order runs first. Classifiers without dispatch_order sort
30
- // last (treated as +Infinity) — useful for "run me whenever there's a slot".
34
+ return manifests;
35
+ }
36
+ // Build a complete classifier registry from the bundled built-ins plus any
37
+ // extra directories supplied by the caller. Sorts by dispatch_order
38
+ // ascending (manifests without dispatch_order sort last). Rejects duplicate
39
+ // names.
40
+ //
41
+ // Mandatory built-ins (preflight, model_tier, model_specialization,
42
+ // prompt_injection) always load. Extras with the same name as a built-in
43
+ // throw — there's no override mechanism. Customise by editing the bundled
44
+ // manifest in your own fork, or replace behaviour entirely with a custom
45
+ // `runClassifier`.
46
+ export function buildClassifierRegistry(options = {}) {
47
+ const manifests = [
48
+ ...loadClassifierRegistry(BUILTIN_CLASSIFIERS_DIR),
49
+ ...(options.extraDirs ?? []).flatMap((dir) => loadClassifierRegistry(dir)),
50
+ ];
31
51
  manifests.sort((a, b) => (a.dispatch_order ?? Infinity) - (b.dispatch_order ?? Infinity));
32
52
  validateRegistry(manifests);
33
- return manifests;
53
+ const registry = manifests;
54
+ const modulesByName = Object.fromEntries(manifests.map((m) => [m.name, m]));
55
+ const names = manifests.map((m) => m.name);
56
+ return { registry, modulesByName, names };
34
57
  }
35
58
  function loadClassifierManifest(classifierDir) {
36
59
  const manifestPath = join(classifierDir, "manifest.json");
@@ -69,18 +92,11 @@ function validateRegistry(manifests) {
69
92
  const names = new Set();
70
93
  for (const manifest of manifests) {
71
94
  if (names.has(manifest.name)) {
72
- throw new ClassifierManifestError(`duplicate classifier name: ${manifest.name}`);
95
+ throw new ClassifierManifestError(`duplicate classifier name: ${manifest.name} — extras cannot override built-ins or other extras. Rename your classifier or run it under a different name.`);
73
96
  }
74
97
  names.add(manifest.name);
75
98
  }
76
99
  }
77
- export const REGISTRY = loadClassifierRegistry();
78
- export const CLASSIFIER_NAMES = REGISTRY.map((m) => m.name);
79
- export const MODULES_BY_NAME = Object.fromEntries(REGISTRY.map((m) => [m.name, m]));
80
- export function validateClassifierOutput(name, value, model) {
81
- const manifest = MODULES_BY_NAME[name];
82
- if (!manifest) {
83
- throw new ClassifierManifestError(`unknown classifier: ${name}`);
84
- }
85
- return validateOutputForManifest(manifest, value, { classifier: name, model });
100
+ export function validateClassifierOutput(manifest, value, model) {
101
+ return validateOutputForManifest(manifest, value, { classifier: manifest.name, model });
86
102
  }
@@ -1,4 +1,4 @@
1
- import { type RunClassifier } from "./classifiers.js";
1
+ import { ClassifierManifestError, type ClassifierRegistryBundle, type RunClassifier } from "./classifiers.js";
2
2
  import { type OpenClassifyConfig } from "./config.js";
3
3
  import type { Catalog, InspectResult, PipelineResult } from "./manifest.js";
4
4
  import type { OpenClassifyInput } from "./types.js";
@@ -11,10 +11,12 @@ export type Inspector = (input: OpenClassifyInput, options?: {
11
11
  export interface OpenClassify {
12
12
  readonly classify: Classifier;
13
13
  readonly inspect: Inspector;
14
+ readonly registry: ClassifierRegistryBundle;
14
15
  }
15
16
  export interface CreateClassifierOptions {
16
17
  runClassifier?: RunClassifier;
17
18
  catalog?: Catalog;
19
+ extraClassifierDirs?: ReadonlyArray<string>;
18
20
  config?: OpenClassifyConfig;
19
21
  configPath?: string;
20
22
  catalogPath?: string;
@@ -27,3 +29,4 @@ export interface CreateClassifierOptions {
27
29
  maxConcurrency?: number;
28
30
  }
29
31
  export declare function createClassifier(options?: CreateClassifierOptions): OpenClassify;
32
+ export { ClassifierManifestError };
@@ -1,9 +1,11 @@
1
- // High-level facade for the pipeline. Builds the runner and catalog once,
2
- // then returns two functions — classify() for the user-input/routing pass
3
- // and inspect() for the assistant-output lean pass. Backend-agnostic: pass a
4
- // custom `runClassifier` to bypass the bundled Ollama runner entirely.
1
+ // High-level facade for the pipeline. Builds the runner, registry, and
2
+ // catalog once, then returns two functions — classify() for the
3
+ // user-input/routing pass and inspect() for the assistant-output lean pass.
4
+ // Backend-agnostic: pass a custom `runClassifier` to bypass the bundled
5
+ // Ollama runner entirely.
5
6
  import { loadCatalog } from "./catalog.js";
6
- import { classifierModelsFromConfig, loadOpenClassifyConfig, } from "./config.js";
7
+ import { buildClassifierRegistry, ClassifierManifestError, } from "./classifiers.js";
8
+ import { classifierModelsFromConfig, loadOpenClassifyConfig, OpenClassifyConfigError, } from "./config.js";
7
9
  import { assertOllamaResources, createOllamaClassifierRunner, OLLAMA_DEFAULT_CATALOG_PATH, } from "./ollama.js";
8
10
  import { classifyOpenClassifyInput, inspectOpenClassifyInput, } from "./pipeline.js";
9
11
  export function createClassifier(options = {}) {
@@ -12,6 +14,20 @@ export function createClassifier(options = {}) {
12
14
  optional: options.configPath === undefined &&
13
15
  process.env.OPEN_CLASSIFY_CONFIG === undefined,
14
16
  });
17
+ const registryBundle = buildClassifierRegistry({
18
+ extraDirs: options.extraClassifierDirs,
19
+ });
20
+ // Cross-check `runner.models` keys against the loaded registry so a typo
21
+ // or stale reference fails fast at construction time instead of being
22
+ // silently ignored by the runner.
23
+ if (fileConfig?.runner?.models !== undefined) {
24
+ const known = new Set(registryBundle.names);
25
+ for (const name of Object.keys(fileConfig.runner.models)) {
26
+ if (!known.has(name)) {
27
+ throw new OpenClassifyConfigError(`runner.models.${name} is not a loaded classifier (loaded: ${registryBundle.names.join(", ")})`);
28
+ }
29
+ }
30
+ }
15
31
  // When we own the runner, hoist the resource check to the wrapper so a
16
32
  // failure surfaces as a top-level rejection — the per-classifier fallback
17
33
  // path would otherwise mask it as five "classifier failed" entries.
@@ -19,6 +35,7 @@ export function createClassifier(options = {}) {
19
35
  const needsResourceCheck = ownsRunner && !options.skipResourceCheck;
20
36
  const runClassifier = options.runClassifier ??
21
37
  createOllamaClassifierRunner({
38
+ modulesByName: registryBundle.modulesByName,
22
39
  host: fileConfig?.runner?.host,
23
40
  defaultModel: fileConfig?.runner?.defaultModel,
24
41
  models: classifierModelsFromConfig(fileConfig),
@@ -43,6 +60,7 @@ export function createClassifier(options = {}) {
43
60
  return classifyOpenClassifyInput(input, {
44
61
  runClassifier,
45
62
  catalog,
63
+ registry: registryBundle.registry,
46
64
  classifierTimeoutMs: options.classifierTimeoutMs,
47
65
  classifierRetryCount: options.classifierRetryCount,
48
66
  maxConcurrency: options.maxConcurrency,
@@ -53,11 +71,15 @@ export function createClassifier(options = {}) {
53
71
  await ensureResources();
54
72
  return inspectOpenClassifyInput(input, {
55
73
  runClassifier,
74
+ registry: registryBundle.registry,
56
75
  classifierTimeoutMs: options.classifierTimeoutMs,
57
76
  classifierRetryCount: options.classifierRetryCount,
58
77
  maxConcurrency: options.maxConcurrency,
59
78
  signal: callOptions?.signal,
60
79
  });
61
80
  };
62
- return { classify, inspect };
81
+ return { classify, inspect, registry: registryBundle };
63
82
  }
83
+ // Re-export so callers can `import { ClassifierManifestError } from "open-classify"`
84
+ // and catch directory/name collision errors from createClassifier().
85
+ export { ClassifierManifestError };
@@ -1,4 +1,4 @@
1
- import { type ClassifierName } from "./classifiers.js";
1
+ import type { ClassifierName } from "./classifiers.js";
2
2
  export declare const DEFAULT_OPEN_CLASSIFY_CONFIG_PATH = "open-classify.config.json";
3
3
  export interface OpenClassifyConfig {
4
4
  readonly runner?: OllamaRunnerConfig;
@@ -1,5 +1,4 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
- import { CLASSIFIER_NAMES } from "./classifiers.js";
3
2
  import { isRecord } from "./validation.js";
4
3
  export const DEFAULT_OPEN_CLASSIFY_CONFIG_PATH = "open-classify.config.json";
5
4
  export class OpenClassifyConfigError extends Error {
@@ -81,12 +80,8 @@ function validateModels(value, path) {
81
80
  if (!isRecord(value)) {
82
81
  throwConfig(path, "runner.models must be an object");
83
82
  }
84
- const allowed = new Set(CLASSIFIER_NAMES);
85
83
  const out = {};
86
84
  for (const [name, model] of Object.entries(value)) {
87
- if (!allowed.has(name)) {
88
- throwConfig(path, `runner.models.${name} is not a known classifier`);
89
- }
90
85
  out[name] = requireString(model, path, `runner.models.${name}`);
91
86
  }
92
87
  return out;
@@ -1,13 +1,11 @@
1
- import { type ClassifierName, type RunClassifier } from "./classifiers.js";
1
+ import { type ClassifierModuleMap, type ClassifierName, type RunClassifier } from "./classifiers.js";
2
2
  export declare const OLLAMA_DEFAULT_HOST = "http://localhost:11434";
3
3
  export declare const OLLAMA_BASE_MODEL = "gemma4:e4b-it-q4_K_M";
4
4
  export declare const OLLAMA_BASE_MODEL_NATIVE_CONTEXT_LENGTH = 131072;
5
- export declare const OLLAMA_REQUIRED_PARALLELISM: number;
6
5
  export declare const OLLAMA_DEFAULT_CATALOG_PATH = "downstream-models.json";
7
6
  export declare const OLLAMA_CONTEXT_LENGTH = 4096;
8
7
  export declare const OLLAMA_MIN_TOTAL_MEMORY_BYTES: number;
9
8
  export declare const OLLAMA_MIN_AVAILABLE_MEMORY_BYTES: number;
10
- export declare const OLLAMA_CLASSIFIER_MODELS: Record<ClassifierName, string | null>;
11
9
  export interface OllamaOptions {
12
10
  temperature?: number;
13
11
  top_p?: number;
@@ -15,14 +13,15 @@ export interface OllamaOptions {
15
13
  num_ctx?: number;
16
14
  }
17
15
  export interface OllamaClassifierRunnerConfig {
16
+ modulesByName: ClassifierModuleMap;
17
+ minTotalMemoryBytes?: number;
18
+ minAvailableMemoryBytes?: number;
18
19
  host?: string;
19
20
  defaultModel?: string;
20
21
  models?: Partial<Record<ClassifierName, string | null>>;
21
22
  options?: OllamaOptions;
22
23
  fetch?: typeof fetch;
23
24
  skipResourceCheck?: boolean;
24
- minAvailableMemoryBytes?: number;
25
- minTotalMemoryBytes?: number;
26
25
  }
27
26
  export declare class OllamaClassifierError extends Error {
28
27
  readonly classifier: ClassifierName;
@@ -36,7 +35,7 @@ export declare class OllamaResourceError extends Error {
36
35
  readonly minAvailableMemoryBytes: number;
37
36
  constructor(totalMemoryBytes: number, availableMemoryBytes: number, minTotalMemoryBytes: number, minAvailableMemoryBytes: number);
38
37
  }
39
- export declare function createOllamaClassifierRunner(config?: OllamaClassifierRunnerConfig): RunClassifier;
38
+ export declare function createOllamaClassifierRunner(config: OllamaClassifierRunnerConfig): RunClassifier;
40
39
  export declare function assertOllamaResources(options?: {
41
40
  minTotalMemoryBytes?: number;
42
41
  minAvailableMemoryBytes?: number;
@@ -10,12 +10,11 @@
10
10
  // `classifyOpenClassifyInput` — you don't have to use this module at all.
11
11
  import { execFile } from "node:child_process";
12
12
  import { promisify } from "node:util";
13
- import { CLASSIFIER_NAMES, MODULES_BY_NAME, validateClassifierOutput, } from "./classifiers.js";
13
+ import { validateClassifierOutput, } from "./classifiers.js";
14
14
  import { ClassifierValidationError, isRecord, } from "./validation.js";
15
15
  export const OLLAMA_DEFAULT_HOST = "http://localhost:11434";
16
16
  export const OLLAMA_BASE_MODEL = "gemma4:e4b-it-q4_K_M";
17
17
  export const OLLAMA_BASE_MODEL_NATIVE_CONTEXT_LENGTH = 131_072;
18
- export const OLLAMA_REQUIRED_PARALLELISM = CLASSIFIER_NAMES.length;
19
18
  export const OLLAMA_DEFAULT_CATALOG_PATH = "downstream-models.json";
20
19
  /*
21
20
  * Gemma 4 E4B's native context is 131,072 tokens (128K). The reference local
@@ -28,7 +27,6 @@ export const OLLAMA_MIN_TOTAL_MEMORY_BYTES = 16 * 1024 * 1024 * 1024;
28
27
  export const OLLAMA_MIN_AVAILABLE_MEMORY_BYTES = 16 * 1024 * 1024 * 1024;
29
28
  const ESTIMATED_CHARS_PER_TOKEN = 3;
30
29
  const execFileAsync = promisify(execFile);
31
- export const OLLAMA_CLASSIFIER_MODELS = Object.fromEntries(CLASSIFIER_NAMES.map((name) => [name, null]));
32
30
  export class OllamaClassifierError extends Error {
33
31
  classifier;
34
32
  model;
@@ -45,7 +43,7 @@ export class OllamaResourceError extends Error {
45
43
  minTotalMemoryBytes;
46
44
  minAvailableMemoryBytes;
47
45
  constructor(totalMemoryBytes, availableMemoryBytes, minTotalMemoryBytes, minAvailableMemoryBytes) {
48
- super(`Ollama resource check failed: ${formatBytes(totalMemoryBytes)} total and ${formatBytes(availableMemoryBytes)} available; ${formatBytes(minTotalMemoryBytes)} total and ${formatBytes(minAvailableMemoryBytes)} available required for ${OLLAMA_REQUIRED_PARALLELISM} parallel classifiers`);
46
+ super(`Ollama resource check failed: ${formatBytes(totalMemoryBytes)} total and ${formatBytes(availableMemoryBytes)} available; ${formatBytes(minTotalMemoryBytes)} total and ${formatBytes(minAvailableMemoryBytes)} available required to run classifiers in parallel`);
49
47
  this.name = "OllamaResourceError";
50
48
  this.totalMemoryBytes = totalMemoryBytes;
51
49
  this.availableMemoryBytes = availableMemoryBytes;
@@ -56,7 +54,11 @@ export class OllamaResourceError extends Error {
56
54
  // Build a `RunClassifier` bound to a specific Ollama host + model selection.
57
55
  // The resource check is lazy and runs once per runner — the first classifier
58
56
  // invocation pays for it; subsequent ones reuse the same promise.
59
- export function createOllamaClassifierRunner(config = {}) {
57
+ export function createOllamaClassifierRunner(config) {
58
+ if (!config?.modulesByName) {
59
+ throw new Error("createOllamaClassifierRunner requires `modulesByName` from buildClassifierRegistry()");
60
+ }
61
+ const modulesByName = config.modulesByName;
60
62
  const host = trimTrailingSlash(config.host ?? OLLAMA_DEFAULT_HOST);
61
63
  const fetchImpl = config.fetch ?? fetch;
62
64
  const models = config.models ?? {};
@@ -76,9 +78,13 @@ export function createOllamaClassifierRunner(config = {}) {
76
78
  });
77
79
  await resourceCheck;
78
80
  }
81
+ const manifest = modulesByName[name];
82
+ if (manifest === undefined) {
83
+ throw new OllamaClassifierError(name, defaultModel, `unknown classifier "${name}" — not present in registry`);
84
+ }
79
85
  const configuredModel = models[name];
80
86
  const model = configuredModel ?? defaultModel;
81
- return runOllamaClassifier(name, input, signal, fetchImpl, host, model, options, configuredModel === undefined && !hasDefaultModelOverride);
87
+ return runOllamaClassifier(manifest, input, signal, fetchImpl, host, model, options, configuredModel === undefined && !hasDefaultModelOverride);
82
88
  };
83
89
  }
84
90
  export async function assertOllamaResources(options = {}) {
@@ -90,10 +96,10 @@ export async function assertOllamaResources(options = {}) {
90
96
  throw new OllamaResourceError(totalMemoryBytes, availableMemoryBytes, minTotalMemoryBytes, minAvailableMemoryBytes);
91
97
  }
92
98
  }
93
- async function runOllamaClassifier(name, input, signal, fetchImpl, host, model, options, allowManifestModel) {
94
- const module_ = MODULES_BY_NAME[name];
95
- const systemPrompt = module_.systemPrompt;
96
- const configuredBaseModel = module_.backend?.ollama?.base_model;
99
+ async function runOllamaClassifier(manifest, input, signal, fetchImpl, host, model, options, allowManifestModel) {
100
+ const name = manifest.name;
101
+ const systemPrompt = manifest.systemPrompt;
102
+ const configuredBaseModel = manifest.backend?.ollama?.base_model;
97
103
  if (allowManifestModel && configuredBaseModel) {
98
104
  model = configuredBaseModel;
99
105
  }
@@ -137,7 +143,7 @@ async function runOllamaClassifier(name, input, signal, fetchImpl, host, model,
137
143
  }
138
144
  const parsed = parseJsonObject(content, name, model);
139
145
  try {
140
- return validateClassifierOutput(name, parsed, model);
146
+ return validateClassifierOutput(manifest, parsed, model);
141
147
  }
142
148
  catch (error) {
143
149
  if (error instanceof ClassifierValidationError) {
@@ -1,5 +1,5 @@
1
1
  import { type RunClassifier } from "./classifiers.js";
2
- import type { Catalog, InspectResult, PipelineResult } from "./manifest.js";
2
+ import type { Catalog, ClassifierRegistry, InspectResult, PipelineResult } from "./manifest.js";
3
3
  import type { OpenClassifyInput } from "./types.js";
4
4
  export declare const DEFAULT_CLASSIFIER_TIMEOUT_MS = 15000;
5
5
  export declare const DEFAULT_CLASSIFIER_RETRY_COUNT = 1;
@@ -10,6 +10,7 @@ export declare class OpenClassifyNormalizationError extends Error {
10
10
  export interface ClassifyOptions {
11
11
  runClassifier: RunClassifier;
12
12
  catalog: Catalog;
13
+ registry: ClassifierRegistry;
13
14
  classifierTimeoutMs?: number;
14
15
  classifierRetryCount?: number;
15
16
  maxConcurrency?: number;
@@ -17,6 +18,7 @@ export interface ClassifyOptions {
17
18
  }
18
19
  export interface InspectOptions {
19
20
  runClassifier: RunClassifier;
21
+ registry: ClassifierRegistry;
20
22
  classifierTimeoutMs?: number;
21
23
  classifierRetryCount?: number;
22
24
  maxConcurrency?: number;
@@ -1,5 +1,4 @@
1
1
  import { assembleResult, buildPublicOutputs } from "./aggregator.js";
2
- import { MODULES_BY_NAME, REGISTRY, } from "./classifiers.js";
3
2
  import { normalizeOpenClassifyInput, toClassifierInput } from "./input.js";
4
3
  export const DEFAULT_CLASSIFIER_TIMEOUT_MS = 15_000;
5
4
  export const DEFAULT_CLASSIFIER_RETRY_COUNT = 1;
@@ -12,7 +11,7 @@ export class OpenClassifyNormalizationError extends Error {
12
11
  }
13
12
  export async function classifyOpenClassifyInput(input, options) {
14
13
  const { request, results, failedClassifiers } = await runPipeline(input, "user", options);
15
- const reg = filteredRegistry("user");
14
+ const reg = filteredRegistry(options.registry, "user");
16
15
  const assembled = assembleResult({
17
16
  registry: reg,
18
17
  results,
@@ -26,7 +25,7 @@ export async function classifyOpenClassifyInput(input, options) {
26
25
  }
27
26
  export async function inspectOpenClassifyInput(input, options) {
28
27
  const { request, results } = await runPipeline(input, "assistant", options);
29
- const reg = filteredRegistry("assistant");
28
+ const reg = filteredRegistry(options.registry, "assistant");
30
29
  const lastMsg = request.messages[request.messages.length - 1];
31
30
  return {
32
31
  target_message_hash: request.target_message_hash,
@@ -56,19 +55,19 @@ async function runPipeline(input, role, options) {
56
55
  const classifierTimeoutMs = options.classifierTimeoutMs ?? DEFAULT_CLASSIFIER_TIMEOUT_MS;
57
56
  const classifierRetryCount = options.classifierRetryCount ?? DEFAULT_CLASSIFIER_RETRY_COUNT;
58
57
  const maxConcurrency = resolveMaxConcurrency(options.maxConcurrency);
59
- const registry = filteredRegistry(role);
58
+ const registry = filteredRegistry(options.registry, role);
60
59
  const queue = registry.map((m) => m.name);
61
60
  try {
62
61
  const settled = await runWithConcurrency(queue, maxConcurrency, controller.signal, (name) => runClassifierWithRetry(name, classifierInput, options.runClassifier, controller.signal, classifierTimeoutMs, classifierRetryCount));
63
- const { results, failedClassifiers } = collectResults(settled);
62
+ const { results, failedClassifiers } = collectResults(registry, settled);
64
63
  return { request, results, failedClassifiers };
65
64
  }
66
65
  finally {
67
66
  options.signal?.removeEventListener("abort", abortFromOptions);
68
67
  }
69
68
  }
70
- function filteredRegistry(role) {
71
- return REGISTRY.filter((m) => roleAppliesTo(m.appliesTo, role));
69
+ function filteredRegistry(registry, role) {
70
+ return registry.filter((m) => roleAppliesTo(m.appliesTo, role));
72
71
  }
73
72
  function roleAppliesTo(appliesTo, role) {
74
73
  return appliesTo === "both" || appliesTo === role;
@@ -107,12 +106,18 @@ async function runWithConcurrency(names, maxConcurrency, signal, start) {
107
106
  await Promise.all(Array.from({ length: workerCount }, () => worker()));
108
107
  return results;
109
108
  }
110
- function collectResults(settled) {
109
+ function collectResults(registry, settled) {
110
+ const fallbackByName = new Map();
111
+ for (const m of registry)
112
+ fallbackByName.set(m.name, m.fallback);
111
113
  const results = {};
112
114
  const failedClassifiers = [];
113
115
  for (const s of settled) {
114
- const manifest = MODULES_BY_NAME[s.name];
115
- results[s.name] = s.ok ? s.value : manifest.fallback;
116
+ const fallback = fallbackByName.get(s.name);
117
+ if (fallback === undefined) {
118
+ throw new Error(`pipeline: classifier "${s.name}" missing from registry`);
119
+ }
120
+ results[s.name] = s.ok ? s.value : fallback;
116
121
  if (!s.ok)
117
122
  failedClassifiers.push(s.name);
118
123
  }
@@ -1,20 +1,27 @@
1
1
  # Adding a classifier
2
2
 
3
- Every classifier — reserved-field-bearing or pure custom — uses the same two-file layout. There is no separate "stock" vs "custom" distinction; the runtime only cares about which reserved fields a classifier opts into.
3
+ Every classifier — bundled or your own — uses the same two-file layout. There is no separate "stock" vs "custom" distinction; the runtime only cares about which reserved fields a classifier opts into.
4
+
5
+ There are two places a classifier can live:
6
+
7
+ - **In your own app**, in a directory you pass to `extraClassifierDirs` (almost always `./classifiers/` after `npx open-classify init`). This is the right path when you've installed Open Classify as a dependency.
8
+ - **In this repo**, under `src/classifiers/<name>/`. Only do this when you're contributing a new mandatory built-in back to Open Classify.
9
+
10
+ Either way, the layout and contract are identical.
4
11
 
5
12
  ## 1. Create the directory
6
13
 
7
14
  ```
8
- src/classifiers/<name>/
15
+ classifiers/<name>/
9
16
  ├── manifest.json
10
17
  └── prompt.md
11
18
  ```
12
19
 
13
- The directory name must match `manifest.json`'s `name` field. Top-level directories starting with `_` (like `_prompts/`) are reserved for shared assets and skipped by the loader.
20
+ The directory name must match `manifest.json`'s `name` field. Directories starting with `_` are skipped by the loader that's the deactivation mechanism (`_topic_tags/` is inert; rename to `topic_tags/` to activate).
14
21
 
15
22
  ## 2. Write the manifest
16
23
 
17
- Minimal example — a pure-custom classifier that emits tags. You don't need to provide JSON examples; the runtime synthesizes one from your schema and shows it to the model.
24
+ Minimal example — a pure-custom classifier that emits tags. The runtime synthesizes a JSON example from your schema, so you don't need to write one.
18
25
 
19
26
  ```json
20
27
  {
@@ -39,8 +46,6 @@ Minimal example — a pure-custom classifier that emits tags. You don't need to
39
46
  }
40
47
  ```
41
48
 
42
- If your classifier's behavior is nuanced enough that hand-picked examples would help the model (preflight is one), add an `output_schema.examples` array. The runtime validates each example against the composed schema at load time, so a broken example fails the build.
43
-
44
49
  To also influence routing, opt into a reserved field:
45
50
 
46
51
  ```json
@@ -73,6 +78,7 @@ Rules:
73
78
  - `reason` and `certainty` are added to the composed schema by the runtime — don't declare them.
74
79
  - `fallback` must validate against the composed schema. Only `reason` and `certainty` are required in fallback; reserved fields and `output_schema.required` fields are exempt (a "no signal" fallback usually omits them).
75
80
  - `output_schema.examples` (JSON Schema standard) must validate against the composed schema at load time, so a broken example fails the build, not the model call.
81
+ - **Name collisions throw.** Extras cannot override the mandatory built-ins (`preflight`, `model_tier`, `model_specialization`, `prompt_injection`). To customize one of those, use a custom `RunClassifier` to intercept it (see "Replacing the backend" below).
76
82
 
77
83
  See [manifests.md](manifests.md) for the full field list.
78
84
 
@@ -90,24 +96,37 @@ Do not invent tags for vague or ambiguous messages.
90
96
 
91
97
  Don't paste enum values for reserved fields — the runtime injects them with canonical wording so they never drift from `src/enums.ts`.
92
98
 
93
- ## 4. Build and test
99
+ ## 4. Use it
94
100
 
95
- ```sh
96
- npm run build # validates the manifest, composes the schema, copies assets
97
- npm test
98
- ```
101
+ After `npx open-classify init`, your `classifiers/` directory already exists. Drop your folder in and point `createClassifier` at the parent dir:
99
102
 
100
- If the manifest is malformed, the loader throws `ClassifierManifestError` with the path and a specific reason.
103
+ ```ts
104
+ import { createClassifier } from "open-classify";
101
105
 
102
- ## 5. Consume the output
106
+ const { classify } = createClassifier({
107
+ extraClassifierDirs: ["./classifiers"],
108
+ });
109
+
110
+ const result = await classify({
111
+ messages: [{ role: "user", text: "Can you review the attached contract?" }],
112
+ });
103
113
 
104
- ```ts
105
- const { classify } = createClassifier({ catalog });
106
- const result = await classify(input);
107
114
  const tags = result.classifier_outputs.topic_tags?.tags ?? [];
108
115
  ```
109
116
 
110
- `classifier_outputs[name]` includes all payload fields plus `reason` (string) and `certainty` (float).
117
+ > Production tip: `"./classifiers"` resolves against `process.cwd()`, which is fine for `npm start` but breaks if the process launches from a different directory. For long-running services, resolve absolutely via `fileURLToPath(import.meta.url) + path.resolve(...)`.
118
+
119
+ If the manifest is malformed, `createClassifier` throws `ClassifierManifestError` at startup with the path and a specific reason — typos fail loud.
120
+
121
+ ## Activating one of the bundled templates
122
+
123
+ `npx open-classify init` copies four templates (`tools`, `memory_retrieval_queries`, `conversation_digest`, `context_shift`) into your `classifiers/` directory as `_<name>/` — inactive because of the underscore prefix. To turn one on:
124
+
125
+ ```sh
126
+ mv classifiers/_tools classifiers/tools
127
+ ```
128
+
129
+ Edit `manifest.json` first if you need to (`tools` in particular ships with an opinionated `allowed_tools` list you'll almost certainly want to tailor). The reverse works on any classifier: rename `<name>/` → `_<name>/` to deactivate without deleting.
111
130
 
112
131
  ## Targeting the assistant response
113
132
 
@@ -134,7 +153,7 @@ The built-in `prompt_injection` ships tagged `"both"` so it runs on both sides.
134
153
 
135
154
  ## Choosing the classifier model
136
155
 
137
- For apps and OSS installs, prefer `open-classify.config.json`:
156
+ In `open-classify.config.json`:
138
157
 
139
158
  ```json
140
159
  {
@@ -148,9 +167,9 @@ For apps and OSS installs, prefer `open-classify.config.json`:
148
167
  }
149
168
  ```
150
169
 
151
- `runner.defaultModel` applies to every classifier without an override. `runner.models` is a flat map keyed by classifier name — there is no separate stock/custom split.
170
+ `runner.defaultModel` applies to every classifier without an override. `runner.models` is a flat map keyed by classifier name — works for built-ins, templates, and your own.
152
171
 
153
- Classifier manifests may also carry an Ollama hint for packaged classifiers:
172
+ Classifier manifests may also carry an Ollama hint:
154
173
 
155
174
  ```json
156
175
  {
@@ -162,15 +181,17 @@ Config file and function options take precedence over manifest hints.
162
181
 
163
182
  ## Replacing the backend
164
183
 
165
- For full backend control, implement your own `RunClassifier` and pass it to `classifyOpenClassifyInput`:
184
+ For full backend control — including replacing a mandatory built-in like `preflight` — implement your own `RunClassifier` and pass it to `createClassifier`:
166
185
 
167
186
  ```ts
168
- import { classifyOpenClassifyInput, loadCatalog } from "open-classify";
187
+ import { createClassifier, type RunClassifier } from "open-classify";
169
188
 
170
189
  const runClassifier: RunClassifier = async (name, input, signal) => {
171
- // call OpenAI, Anthropic, a remote service, etc.
172
- // return a ClassifierOutput matching the classifier's composed schema.
190
+ if (name === "preflight") {
191
+ // call OpenAI / Anthropic / your own logic; return a ClassifierOutput.
192
+ }
193
+ // …handle other classifiers, or delegate to the Ollama runner you imported.
173
194
  };
174
195
 
175
- await classifyOpenClassifyInput(input, { runClassifier, catalog: loadCatalog(...) });
196
+ const { classify } = createClassifier({ runClassifier });
176
197
  ```
@@ -13,9 +13,7 @@
13
13
  "preflight": "gemma4:e4b-it-q4_K_M",
14
14
  "model_tier": "gemma4:e4b-it-q4_K_M",
15
15
  "model_specialization": "gemma4:e4b-it-q4_K_M",
16
- "tools": "gemma4:e4b-it-q4_K_M",
17
- "prompt_injection": "gemma4:e4b-it-q4_K_M",
18
- "memory_retrieval_queries": "gemma4:e4b-it-q4_K_M"
16
+ "prompt_injection": "gemma4:e4b-it-q4_K_M"
19
17
  }
20
18
  },
21
19
  "catalog": "downstream-models.json"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "open-classify",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Manifest-driven classifier runtime for routing user messages to downstream AI models",
5
5
  "license": "MIT",
6
6
  "author": "Taylor Bayouth",
@@ -29,11 +29,16 @@
29
29
  "default": "./dist/src/index.js"
30
30
  }
31
31
  },
32
+ "bin": {
33
+ "open-classify": "./bin/open-classify.mjs"
34
+ },
32
35
  "files": [
36
+ "bin",
33
37
  "dist/src",
34
38
  "docs",
35
39
  "downstream-models.json",
36
40
  "open-classify.config.example.json",
41
+ "templates",
37
42
  "LICENSE",
38
43
  "README.md"
39
44
  ],