nexus-ai-pro 0.2.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.
Files changed (171) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1249 -0
  3. package/assets/nexus-ai-pro-presentation.jpg +0 -0
  4. package/dist/agent/loop.d.ts +12 -0
  5. package/dist/agent/loop.js +89 -0
  6. package/dist/agent/tool.d.ts +16 -0
  7. package/dist/agent/tool.js +43 -0
  8. package/dist/cache/adapters.d.ts +42 -0
  9. package/dist/cache/adapters.js +76 -0
  10. package/dist/cache/memory-cache.d.ts +20 -0
  11. package/dist/cache/memory-cache.js +63 -0
  12. package/dist/cache/semantic-cache.d.ts +19 -0
  13. package/dist/cache/semantic-cache.js +71 -0
  14. package/dist/connectors/web.d.ts +8 -0
  15. package/dist/connectors/web.js +60 -0
  16. package/dist/core/nexus.d.ts +71 -0
  17. package/dist/core/nexus.js +477 -0
  18. package/dist/core/response-format.d.ts +8 -0
  19. package/dist/core/response-format.js +121 -0
  20. package/dist/core/secure-stream.d.ts +3 -0
  21. package/dist/core/secure-stream.js +41 -0
  22. package/dist/core/streaming.d.ts +4 -0
  23. package/dist/core/streaming.js +30 -0
  24. package/dist/embeddings/providers.d.ts +20 -0
  25. package/dist/embeddings/providers.js +60 -0
  26. package/dist/evals/metrics.d.ts +75 -0
  27. package/dist/evals/metrics.js +157 -0
  28. package/dist/evals/runner.d.ts +35 -0
  29. package/dist/evals/runner.js +47 -0
  30. package/dist/hallucination/consistency.d.ts +14 -0
  31. package/dist/hallucination/consistency.js +58 -0
  32. package/dist/hallucination/factual.d.ts +14 -0
  33. package/dist/hallucination/factual.js +35 -0
  34. package/dist/hallucination/knowledge-graph.d.ts +27 -0
  35. package/dist/hallucination/knowledge-graph.js +70 -0
  36. package/dist/hallucination/rag.d.ts +21 -0
  37. package/dist/hallucination/rag.js +46 -0
  38. package/dist/hallucination/retrieval.d.ts +24 -0
  39. package/dist/hallucination/retrieval.js +71 -0
  40. package/dist/hallucination/verification.d.ts +36 -0
  41. package/dist/hallucination/verification.js +110 -0
  42. package/dist/index.d.ts +62 -0
  43. package/dist/index.js +53 -0
  44. package/dist/jobs/batch.d.ts +11 -0
  45. package/dist/jobs/batch.js +32 -0
  46. package/dist/jobs/durable-adapters.d.ts +48 -0
  47. package/dist/jobs/durable-adapters.js +50 -0
  48. package/dist/jobs/queue.d.ts +24 -0
  49. package/dist/jobs/queue.js +57 -0
  50. package/dist/models/registry.d.ts +15 -0
  51. package/dist/models/registry.js +43 -0
  52. package/dist/next/route-handler.d.ts +6 -0
  53. package/dist/next/route-handler.js +35 -0
  54. package/dist/ops/audit-logger.d.ts +6 -0
  55. package/dist/ops/audit-logger.js +19 -0
  56. package/dist/ops/health.d.ts +29 -0
  57. package/dist/ops/health.js +69 -0
  58. package/dist/ops/metrics.d.ts +53 -0
  59. package/dist/ops/metrics.js +122 -0
  60. package/dist/ops/otel-tracing.d.ts +20 -0
  61. package/dist/ops/otel-tracing.js +50 -0
  62. package/dist/ops/rate-limiter.d.ts +11 -0
  63. package/dist/ops/rate-limiter.js +33 -0
  64. package/dist/optimizer/budget.d.ts +19 -0
  65. package/dist/optimizer/budget.js +88 -0
  66. package/dist/optimizer/cost.d.ts +13 -0
  67. package/dist/optimizer/cost.js +31 -0
  68. package/dist/optimizer/densifier.d.ts +10 -0
  69. package/dist/optimizer/densifier.js +77 -0
  70. package/dist/optimizer/index.d.ts +12 -0
  71. package/dist/optimizer/index.js +59 -0
  72. package/dist/pipeline/pipeline.d.ts +14 -0
  73. package/dist/pipeline/pipeline.js +106 -0
  74. package/dist/pipeline/types.d.ts +46 -0
  75. package/dist/pipeline/types.js +1 -0
  76. package/dist/providers/anthropic.d.ts +15 -0
  77. package/dist/providers/anthropic.js +183 -0
  78. package/dist/providers/base.d.ts +19 -0
  79. package/dist/providers/base.js +75 -0
  80. package/dist/providers/cohere.d.ts +16 -0
  81. package/dist/providers/cohere.js +104 -0
  82. package/dist/providers/google.d.ts +22 -0
  83. package/dist/providers/google.js +226 -0
  84. package/dist/providers/groq.d.ts +15 -0
  85. package/dist/providers/groq.js +50 -0
  86. package/dist/providers/mistral.d.ts +15 -0
  87. package/dist/providers/mistral.js +50 -0
  88. package/dist/providers/ollama.d.ts +15 -0
  89. package/dist/providers/ollama.js +108 -0
  90. package/dist/providers/openai.d.ts +21 -0
  91. package/dist/providers/openai.js +304 -0
  92. package/dist/providers/openrouter.d.ts +14 -0
  93. package/dist/providers/openrouter.js +19 -0
  94. package/dist/rag/file-ingestion.d.ts +20 -0
  95. package/dist/rag/file-ingestion.js +55 -0
  96. package/dist/rag/ingestion.d.ts +21 -0
  97. package/dist/rag/ingestion.js +47 -0
  98. package/dist/router/auto-router.d.ts +17 -0
  99. package/dist/router/auto-router.js +228 -0
  100. package/dist/router/failover.d.ts +21 -0
  101. package/dist/router/failover.js +126 -0
  102. package/dist/router/index.d.ts +12 -0
  103. package/dist/router/index.js +31 -0
  104. package/dist/router/rules-router.d.ts +7 -0
  105. package/dist/router/rules-router.js +68 -0
  106. package/dist/router/types.d.ts +19 -0
  107. package/dist/router/types.js +1 -0
  108. package/dist/security/index.d.ts +31 -0
  109. package/dist/security/index.js +173 -0
  110. package/dist/security/injection-calibration.d.ts +17 -0
  111. package/dist/security/injection-calibration.js +48 -0
  112. package/dist/security/injection-detector.d.ts +8 -0
  113. package/dist/security/injection-detector.js +66 -0
  114. package/dist/security/input-guard.d.ts +7 -0
  115. package/dist/security/input-guard.js +109 -0
  116. package/dist/security/output-guard.d.ts +6 -0
  117. package/dist/security/output-guard.js +144 -0
  118. package/dist/security/pii-detector.d.ts +8 -0
  119. package/dist/security/pii-detector.js +66 -0
  120. package/dist/security/policies.d.ts +4 -0
  121. package/dist/security/policies.js +61 -0
  122. package/dist/security/prompt-hardening.d.ts +6 -0
  123. package/dist/security/prompt-hardening.js +18 -0
  124. package/dist/security/schema-validator.d.ts +234 -0
  125. package/dist/security/schema-validator.js +68 -0
  126. package/dist/security/semantic-injection-classifier.d.ts +19 -0
  127. package/dist/security/semantic-injection-classifier.js +75 -0
  128. package/dist/security/upload-scanner.d.ts +29 -0
  129. package/dist/security/upload-scanner.js +72 -0
  130. package/dist/testing/provider-conformance.d.ts +24 -0
  131. package/dist/testing/provider-conformance.js +97 -0
  132. package/dist/types/agent.d.ts +34 -0
  133. package/dist/types/agent.js +1 -0
  134. package/dist/types/config.d.ts +171 -0
  135. package/dist/types/config.js +1 -0
  136. package/dist/types/messages.d.ts +96 -0
  137. package/dist/types/messages.js +1 -0
  138. package/dist/types/optimizer.d.ts +30 -0
  139. package/dist/types/optimizer.js +1 -0
  140. package/dist/types/planning.d.ts +27 -0
  141. package/dist/types/planning.js +1 -0
  142. package/dist/types/providers.d.ts +39 -0
  143. package/dist/types/providers.js +479 -0
  144. package/dist/types/response.d.ts +55 -0
  145. package/dist/types/response.js +1 -0
  146. package/dist/types/security.d.ts +87 -0
  147. package/dist/types/security.js +1 -0
  148. package/dist/utils/ids.d.ts +1 -0
  149. package/dist/utils/ids.js +4 -0
  150. package/dist/utils/logger.d.ts +7 -0
  151. package/dist/utils/logger.js +17 -0
  152. package/dist/utils/tokenizer.d.ts +6 -0
  153. package/dist/utils/tokenizer.js +38 -0
  154. package/dist/workflow/chains.d.ts +53 -0
  155. package/dist/workflow/chains.js +117 -0
  156. package/dist/workflow/domain.d.ts +22 -0
  157. package/dist/workflow/domain.js +96 -0
  158. package/examples/agent.ts +69 -0
  159. package/examples/basic.ts +37 -0
  160. package/examples/bullmq-worker.ts +36 -0
  161. package/examples/classifier-calibration.ts +7 -0
  162. package/examples/domain-workflows.ts +38 -0
  163. package/examples/feature-flags.ts +65 -0
  164. package/examples/minimal.ts +45 -0
  165. package/examples/ocr-pdf-ingestion.ts +42 -0
  166. package/examples/optimizer.ts +43 -0
  167. package/examples/otel-express.ts +47 -0
  168. package/examples/otel-node-http.ts +46 -0
  169. package/examples/persisted-vector-store.ts +42 -0
  170. package/examples/security.ts +69 -0
  171. package/package.json +165 -0
@@ -0,0 +1,46 @@
1
+ export function withRagContext(request, options) {
2
+ const chunks = options.chunks.slice(0, options.maxChunks || options.chunks.length);
3
+ const unknownAnswer = options.unknownAnswer || "I don't know based on the provided context.";
4
+ const citationInstruction = options.requireCitations !== false
5
+ ? 'Cite sources for factual claims using the chunk ids in square brackets, for example [doc-1].'
6
+ : 'Use only the provided context for factual claims.';
7
+ const context = chunks.map((chunk, index) => {
8
+ const id = chunk.id || `chunk-${index + 1}`;
9
+ const source = chunk.source ? ` source=${chunk.source}` : '';
10
+ return `[${id}${source}]\n${chunk.content}`;
11
+ }).join('\n\n');
12
+ const systemMessage = {
13
+ role: 'system',
14
+ content: [
15
+ 'You are answering with Retrieval-Augmented Generation context.',
16
+ 'Use only the provided context for factual claims.',
17
+ `If the answer is not present in the context, say: "${unknownAnswer}"`,
18
+ citationInstruction,
19
+ 'Do not invent sources, citations, facts, numbers, names, URLs, dates, or APIs.',
20
+ 'Context:',
21
+ context || '[no context provided]',
22
+ ].join('\n'),
23
+ };
24
+ return {
25
+ ...request,
26
+ temperature: request.temperature ?? 0,
27
+ topP: request.topP ?? 0.1,
28
+ messages: [systemMessage, ...request.messages],
29
+ metadata: {
30
+ ...request.metadata,
31
+ rag: {
32
+ chunks: chunks.map(({ id, source, score, metadata }) => ({ id, source, score, metadata })),
33
+ requireCitations: options.requireCitations !== false,
34
+ },
35
+ },
36
+ };
37
+ }
38
+ export function extractCitations(text) {
39
+ return [...new Set([...text.matchAll(/\[([^\]]+)\]/g)].map((match) => match[1]))];
40
+ }
41
+ export function validateCitations(responseText, chunks) {
42
+ const known = new Set(chunks.map((chunk) => chunk.id));
43
+ const citations = extractCitations(responseText);
44
+ const missing = citations.filter((citation) => !known.has(citation));
45
+ return { ok: missing.length === 0, missing };
46
+ }
@@ -0,0 +1,24 @@
1
+ import type { RagChunk } from './rag.js';
2
+ export type EmbeddingProvider = (texts: string[]) => Promise<number[][]> | number[][];
3
+ export interface VectorDocument extends RagChunk {
4
+ embedding?: number[];
5
+ }
6
+ export interface VectorSearchResult extends RagChunk {
7
+ score: number;
8
+ }
9
+ export interface VectorSearchOptions {
10
+ topK?: number;
11
+ minScore?: number;
12
+ }
13
+ export declare class MemoryVectorStore {
14
+ private documents;
15
+ private embed;
16
+ constructor(embed?: EmbeddingProvider);
17
+ add(documents: VectorDocument[]): Promise<void>;
18
+ search(query: string, options?: VectorSearchOptions): Promise<VectorSearchResult[]>;
19
+ clear(): void;
20
+ size(): number;
21
+ }
22
+ export declare function createHashEmbeddings(texts: string[], dimensions?: number): number[][];
23
+ export declare function cosineSimilarity(a: number[], b: number[]): number;
24
+ export declare function normalizeVector(vector: number[]): number[];
@@ -0,0 +1,71 @@
1
+ export class MemoryVectorStore {
2
+ documents = [];
3
+ embed;
4
+ constructor(embed = createHashEmbeddings) {
5
+ this.embed = embed;
6
+ }
7
+ async add(documents) {
8
+ const missing = documents.filter((doc) => !doc.embedding).map((doc) => doc.content);
9
+ const generated = missing.length ? await this.embed(missing) : [];
10
+ let generatedIndex = 0;
11
+ for (const doc of documents) {
12
+ const embedding = doc.embedding || generated[generatedIndex++];
13
+ this.documents.push({ ...doc, embedding: normalizeVector(embedding) });
14
+ }
15
+ }
16
+ async search(query, options = {}) {
17
+ const topK = options.topK || 5;
18
+ const minScore = options.minScore ?? 0;
19
+ const [queryEmbedding] = await this.embed([query]);
20
+ const normalizedQuery = normalizeVector(queryEmbedding);
21
+ return this.documents
22
+ .map(({ embedding, ...doc }) => ({
23
+ ...doc,
24
+ score: cosineSimilarity(normalizedQuery, embedding),
25
+ }))
26
+ .filter((doc) => doc.score >= minScore)
27
+ .sort((a, b) => b.score - a.score)
28
+ .slice(0, topK);
29
+ }
30
+ clear() {
31
+ this.documents = [];
32
+ }
33
+ size() {
34
+ return this.documents.length;
35
+ }
36
+ }
37
+ export function createHashEmbeddings(texts, dimensions = 384) {
38
+ return texts.map((text) => {
39
+ const vector = new Array(dimensions).fill(0);
40
+ const terms = text.toLowerCase().match(/[a-z0-9_'-]{2,}/g) || [];
41
+ for (const term of terms) {
42
+ const index = hashTerm(term) % dimensions;
43
+ vector[index] += 1;
44
+ }
45
+ return normalizeVector(vector);
46
+ });
47
+ }
48
+ export function cosineSimilarity(a, b) {
49
+ const length = Math.min(a.length, b.length);
50
+ if (length === 0)
51
+ return 0;
52
+ let dot = 0;
53
+ for (let i = 0; i < length; i += 1) {
54
+ dot += a[i] * b[i];
55
+ }
56
+ return dot;
57
+ }
58
+ export function normalizeVector(vector) {
59
+ const magnitude = Math.sqrt(vector.reduce((sum, value) => sum + value * value, 0));
60
+ if (magnitude === 0)
61
+ return vector.map(() => 0);
62
+ return vector.map((value) => value / magnitude);
63
+ }
64
+ function hashTerm(term) {
65
+ let hash = 2166136261;
66
+ for (let i = 0; i < term.length; i += 1) {
67
+ hash ^= term.charCodeAt(i);
68
+ hash = Math.imul(hash, 16777619);
69
+ }
70
+ return hash >>> 0;
71
+ }
@@ -0,0 +1,36 @@
1
+ import type { CompletionRequest } from '../types/messages.js';
2
+ import type { NexusResponse } from '../types/response.js';
3
+ export interface VerificationClient {
4
+ complete(request: CompletionRequest): Promise<NexusResponse>;
5
+ }
6
+ export interface VerificationFact {
7
+ text: string;
8
+ supported: boolean;
9
+ score: number;
10
+ }
11
+ export interface VerificationReport {
12
+ ok: boolean;
13
+ facts: VerificationFact[];
14
+ unsupported: VerificationFact[];
15
+ supportRatio: number;
16
+ }
17
+ export interface NliVerifier {
18
+ verify(claim: string, context: string): Promise<{
19
+ entailed: boolean;
20
+ score: number;
21
+ }>;
22
+ }
23
+ export interface VerificationOptions {
24
+ context: string[];
25
+ minSupportRatio?: number;
26
+ nli?: NliVerifier;
27
+ unknownAnswer?: string;
28
+ repair?: boolean;
29
+ }
30
+ export declare function completeVerified(client: VerificationClient, request: CompletionRequest, options: VerificationOptions): Promise<NexusResponse>;
31
+ export declare function verifyAgainstContext(answer: string, options: VerificationOptions): Promise<VerificationReport>;
32
+ export declare function extractFacts(answer: string): string[];
33
+ export declare function lexicalEntailment(claim: string, context: string): {
34
+ entailed: boolean;
35
+ score: number;
36
+ };
@@ -0,0 +1,110 @@
1
+ import { withFactualDefaults } from './factual.js';
2
+ export async function completeVerified(client, request, options) {
3
+ const initial = await client.complete(withFactualDefaults(request, {
4
+ requireUnknownFallback: true,
5
+ unknownAnswer: options.unknownAnswer || "I don't know.",
6
+ chainOfThought: 'private',
7
+ }));
8
+ const report = await verifyAgainstContext(initial.content, options);
9
+ if (report.ok || options.repair === false) {
10
+ return attachVerification(initial, report);
11
+ }
12
+ const repaired = await client.complete(withFactualDefaults({
13
+ ...request,
14
+ messages: [
15
+ ...request.messages,
16
+ {
17
+ role: 'assistant',
18
+ content: initial.content,
19
+ },
20
+ {
21
+ role: 'user',
22
+ content: [
23
+ 'Revise the answer so every factual claim is supported by the context.',
24
+ `If unsupported, answer exactly: "${options.unknownAnswer || "I don't know."}"`,
25
+ 'Unsupported claims:',
26
+ ...report.unsupported.map((fact) => `- ${fact.text}`),
27
+ 'Context:',
28
+ ...options.context.map((item, index) => `[context-${index + 1}] ${item}`),
29
+ ].join('\n'),
30
+ },
31
+ ],
32
+ }, {
33
+ requireUnknownFallback: true,
34
+ unknownAnswer: options.unknownAnswer || "I don't know.",
35
+ chainOfThought: 'private',
36
+ }));
37
+ return attachVerification(repaired, await verifyAgainstContext(repaired.content, options));
38
+ }
39
+ export async function verifyAgainstContext(answer, options) {
40
+ const context = options.context.join('\n');
41
+ const facts = extractFacts(answer);
42
+ if (facts.length === 0) {
43
+ return { ok: true, facts: [], unsupported: [], supportRatio: 1 };
44
+ }
45
+ const checked = await Promise.all(facts.map(async (fact) => {
46
+ const result = options.nli
47
+ ? await options.nli.verify(fact, context)
48
+ : lexicalEntailment(fact, context);
49
+ return {
50
+ text: fact,
51
+ supported: result.entailed,
52
+ score: result.score,
53
+ };
54
+ }));
55
+ const supported = checked.filter((fact) => fact.supported).length;
56
+ const supportRatio = supported / checked.length;
57
+ const minSupportRatio = options.minSupportRatio ?? 0.85;
58
+ const unsupported = checked.filter((fact) => !fact.supported);
59
+ return {
60
+ ok: supportRatio >= minSupportRatio && unsupported.length === 0,
61
+ facts: checked,
62
+ unsupported,
63
+ supportRatio,
64
+ };
65
+ }
66
+ export function extractFacts(answer) {
67
+ return answer
68
+ .split(/(?<=[.!?])\s+|\n+/)
69
+ .map((part) => part.trim())
70
+ .filter((part) => part.length > 0)
71
+ .filter((part) => !/^(i don't know|unknown|not enough information)/i.test(part));
72
+ }
73
+ export function lexicalEntailment(claim, context) {
74
+ const claimTerms = importantTerms(claim);
75
+ if (claimTerms.length === 0)
76
+ return { entailed: true, score: 1 };
77
+ const contextTerms = new Set(importantTerms(context));
78
+ const matches = claimTerms.filter((term) => contextTerms.has(term)).length;
79
+ const score = matches / claimTerms.length;
80
+ return {
81
+ entailed: score >= 0.72 || context.toLowerCase().includes(claim.toLowerCase()),
82
+ score,
83
+ };
84
+ }
85
+ function importantTerms(text) {
86
+ const stop = new Set([
87
+ 'about', 'after', 'also', 'because', 'before', 'being', 'could', 'from',
88
+ 'have', 'into', 'only', 'over', 'than', 'that', 'their', 'there', 'these',
89
+ 'this', 'those', 'through', 'under', 'using', 'were', 'when', 'where',
90
+ 'which', 'while', 'with', 'would', 'your',
91
+ ]);
92
+ return (text.toLowerCase().match(/[a-z0-9_'-]{3,}/g) || [])
93
+ .filter((term) => !stop.has(term));
94
+ }
95
+ function attachVerification(response, report) {
96
+ return {
97
+ ...response,
98
+ meta: {
99
+ ...response.meta,
100
+ guardrailsApplied: [...response.meta.guardrailsApplied, 'chain-of-verification'],
101
+ verification: {
102
+ ok: report.ok,
103
+ supportRatio: report.supportRatio,
104
+ factsChecked: report.facts.length,
105
+ unsupportedFacts: report.unsupported.map((fact) => fact.text),
106
+ },
107
+ },
108
+ content: response.content,
109
+ };
110
+ }
@@ -0,0 +1,62 @@
1
+ export { NexusAI } from './core/nexus.js';
2
+ export { collectStream, mapStream, createTextStream } from './core/streaming.js';
3
+ export { createNexusRouteHandler } from './next/route-handler.js';
4
+ export { getModelRegistry, getModelAliases, resolveModel, resolveModelAlias, listKnownModels, listModelsForProvider, getModelCapabilities, } from './models/registry.js';
5
+ export { MemoryCache, createCacheKey } from './cache/memory-cache.js';
6
+ export { MemoryCacheAdapter, RedisCacheAdapter, SQLiteCacheAdapter, type CacheAdapter, type RedisLikeClient, type SQLiteLikeDatabase, } from './cache/adapters.js';
7
+ export { SemanticCache, type SemanticCacheOptions } from './cache/semantic-cache.js';
8
+ export { ResponseFormatError } from './core/response-format.js';
9
+ export { hardenPrompt } from './security/prompt-hardening.js';
10
+ export { RateLimiter, NexusRateLimitError } from './ops/rate-limiter.js';
11
+ export { AuditLogger } from './ops/audit-logger.js';
12
+ export { MetricsCollector, InMemoryMetrics, OpenTelemetryMetricsSink, type MetricsConfig, type MetricsSink, type OpenTelemetryLikeMeter, } from './ops/metrics.js';
13
+ export { ProviderHealthMonitor, type HealthConfig, type ProviderHealthSnapshot, } from './ops/health.js';
14
+ export { PipelineRunner, createPipelineContext, } from './pipeline/pipeline.js';
15
+ export type { PipelineConfig, PipelineContext, PipelineHookName, PipelineHooksConfig, PipelineMiddleware, PipelineStep, PipelineStepName, PipelineTrace, PipelineTraceStep, } from './pipeline/types.js';
16
+ export { withFactualDefaults, asJsonOnly, type FactualOptions, } from './hallucination/factual.js';
17
+ export { withRagContext, extractCitations, validateCitations, type RagChunk, type RagOptions, } from './hallucination/rag.js';
18
+ export { MemoryVectorStore, createHashEmbeddings, cosineSimilarity, type EmbeddingProvider, type VectorDocument, type VectorSearchOptions, type VectorSearchResult, } from './hallucination/retrieval.js';
19
+ export { withKnowledgeGraphContext, selectGraphFacts, type KnowledgeGraph, type KnowledgeGraphEdge, type KnowledgeGraphNode, type KnowledgeGraphOptions, } from './hallucination/knowledge-graph.js';
20
+ export { completeVerified, verifyAgainstContext, extractFacts, lexicalEntailment, type NliVerifier, type VerificationClient, type VerificationFact, type VerificationOptions, type VerificationReport, } from './hallucination/verification.js';
21
+ export { completeWithSelfConsistency, selectMostConsistent, textSimilarity, type ConsistencyClient, type SelfConsistencyOptions, } from './hallucination/consistency.js';
22
+ export { BaseProvider, type ProviderInfo } from './providers/base.js';
23
+ export { OpenAIProvider } from './providers/openai.js';
24
+ export { AnthropicProvider } from './providers/anthropic.js';
25
+ export { GoogleProvider } from './providers/google.js';
26
+ export { OllamaProvider } from './providers/ollama.js';
27
+ export { OpenRouterProvider } from './providers/openrouter.js';
28
+ export { GroqProvider } from './providers/groq.js';
29
+ export { MistralProvider } from './providers/mistral.js';
30
+ export { CohereProvider } from './providers/cohere.js';
31
+ export { Router, FailoverExecutor, type RouteDecision, type RouterContext } from './router/index.js';
32
+ export { SecurityPipeline, NexusSecurityError, SchemaValidator, InjectionDetector, PIIDetector, OutputGuard, InputGuard, SemanticInjectionClassifier, } from './security/index.js';
33
+ export { guardrailPolicy, GUARDRAIL_POLICIES, type GuardrailPolicyName, } from './security/policies.js';
34
+ export { SEMANTIC_INJECTION_CALIBRATION_SET, calibrateSemanticInjectionClassifier, type InjectionCalibrationExample, type InjectionCalibrationResult, } from './security/injection-calibration.js';
35
+ export { TokenOptimizer, PromptDensifier, BudgetEnforcer, TokenBudgetError, } from './optimizer/index.js';
36
+ export { estimateCost, assertWithinCostBudget, CostBudgetError, type CostEstimateInput, } from './optimizer/cost.js';
37
+ export { createOpenAIEmbeddingProvider, createGeminiEmbeddingProvider, createCohereEmbeddingProvider, type OpenAIEmbeddingOptions, type GeminiEmbeddingOptions, type CohereEmbeddingOptions, } from './embeddings/providers.js';
38
+ export { tool, ToolExecutor } from './agent/tool.js';
39
+ export { AgentLoop, type AgentModelClient } from './agent/loop.js';
40
+ export { Tokenizer } from './utils/tokenizer.js';
41
+ export { ingestDocuments, ingestText, type DocumentSource, type IngestionOptions, type IngestionResult, } from './rag/ingestion.js';
42
+ export { ingestFilesAfterScan, createPdfExtractor, createOcrExtractor, type FileTextExtractor, type FileIngestionOptions, type FileIngestionResult, } from './rag/file-ingestion.js';
43
+ export { UploadScanner, scanUploads, type FileUpload, type UploadScannerOptions, type UploadScanFinding, type UploadScanResult, } from './security/upload-scanner.js';
44
+ export { EvalRunner, type EvalCase, type EvalClient, type EvalResult, type EvalRunResult, } from './evals/runner.js';
45
+ export { calculateEvalMetrics, exactMatch, f1Score, semanticSimilarity, passAtK, perplexity, tokensPerSecond, faithfulness, contextualPrecision, contextualRecall, toxicityScore, biasScore, policyAdherence, refusalRate, type EvalMetrics, type MetricInputs, type QualityMetrics, type OperationalMetrics, type RagMetrics, type SafetyMetrics, } from './evals/metrics.js';
46
+ export { summarizeVerifyFormat, ragAnswer, extractStructured, classifyRoute, compareAndDecide, type SummarizeVerifyFormatOptions, type RagAnswerOptions, type ExtractStructuredOptions, type ClassifyRouteOptions, type CompareOptions, type WorkflowClient, type WorkflowResult, type WorkflowStepResult, } from './workflow/chains.js';
47
+ export { supportTriageWorkflow, salesQualificationWorkflow, legalReviewWorkflow, codeReviewWorkflow, type DomainWorkflowOptions, type SupportWorkflowOptions, type SalesWorkflowOptions, type LegalReviewWorkflowOptions, type CodeReviewWorkflowOptions, } from './workflow/domain.js';
48
+ export { runBatch, type BatchOptions, type BatchItemResult, } from './jobs/batch.js';
49
+ export { JobQueue, type QueueJob, type QueueOptions, } from './jobs/queue.js';
50
+ export { RedisQueueAdapter, BullMQQueueAdapter, type DurableQueueAdapter, type RedisQueueLikeClient, type BullMQLikeQueue, } from './jobs/durable-adapters.js';
51
+ export { createFetchUrlTool, createSearchTool, type WebConnectorOptions, } from './connectors/web.js';
52
+ export { PROVIDER_CONFORMANCE_FIXTURES, runProviderConformance, type ProviderConformanceCase, type ProviderConformanceResult, } from './testing/provider-conformance.js';
53
+ export { OpenTelemetryTraceExporter, type OpenTelemetryLikeSpan, type OpenTelemetryLikeTracer, } from './ops/otel-tracing.js';
54
+ export type { CompletionRequest, Message, MessageRole, ContentPart, TextContent, ImageContent, AudioContent, VideoContent, ToolDefinition, ToolCallResult, } from './types/messages.js';
55
+ export type { CostEstimate, NexusPlan, } from './types/planning.js';
56
+ export type { NexusResponse, NexusStream, ResponseMeta, StreamChunk, ToolCall, } from './types/response.js';
57
+ export type { NexusAIConfig, ProvidersConfig, OpenAIProviderConfig, AnthropicProviderConfig, GoogleProviderConfig, OllamaProviderConfig, GroqProviderConfig, MistralProviderConfig, CohereProviderConfig, CustomProviderConfig, RoutingConfig, RoutingRule, RoutingStrategy, FallbackConfig, ResponseFormatConfig, RetryConfig, CostBudgetConfig, AuditLogConfig, RateLimitConfig, } from './types/config.js';
58
+ export type { Modality, ModelCapabilities, ModelEndpoint, ModelStatus, ProviderCapabilities, ReasoningEffort, RoutingModelPreference, } from './types/providers.js';
59
+ export { KNOWN_MODELS, resolveProvider } from './types/providers.js';
60
+ export type { SecurityLevel, SecurityAction, PIIType, InjectionDetectionConfig, PIIConfig, SecurityConfig, SecurityFinding, SecurityResult, } from './types/security.js';
61
+ export type { BudgetExceededAction, DensificationConfig, BudgetConfig, TokenOptimizerConfig, TokenUsageSnapshot, OptimizationResult, } from './types/optimizer.js';
62
+ export type { AgentStep, AgentConfig, AgentResult, ToolExecutionResult, } from './types/agent.js';
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ export { NexusAI } from './core/nexus.js';
2
+ export { collectStream, mapStream, createTextStream } from './core/streaming.js';
3
+ export { createNexusRouteHandler } from './next/route-handler.js';
4
+ export { getModelRegistry, getModelAliases, resolveModel, resolveModelAlias, listKnownModels, listModelsForProvider, getModelCapabilities, } from './models/registry.js';
5
+ export { MemoryCache, createCacheKey } from './cache/memory-cache.js';
6
+ export { MemoryCacheAdapter, RedisCacheAdapter, SQLiteCacheAdapter, } from './cache/adapters.js';
7
+ export { SemanticCache } from './cache/semantic-cache.js';
8
+ export { ResponseFormatError } from './core/response-format.js';
9
+ export { hardenPrompt } from './security/prompt-hardening.js';
10
+ export { RateLimiter, NexusRateLimitError } from './ops/rate-limiter.js';
11
+ export { AuditLogger } from './ops/audit-logger.js';
12
+ export { MetricsCollector, InMemoryMetrics, OpenTelemetryMetricsSink, } from './ops/metrics.js';
13
+ export { ProviderHealthMonitor, } from './ops/health.js';
14
+ export { PipelineRunner, createPipelineContext, } from './pipeline/pipeline.js';
15
+ export { withFactualDefaults, asJsonOnly, } from './hallucination/factual.js';
16
+ export { withRagContext, extractCitations, validateCitations, } from './hallucination/rag.js';
17
+ export { MemoryVectorStore, createHashEmbeddings, cosineSimilarity, } from './hallucination/retrieval.js';
18
+ export { withKnowledgeGraphContext, selectGraphFacts, } from './hallucination/knowledge-graph.js';
19
+ export { completeVerified, verifyAgainstContext, extractFacts, lexicalEntailment, } from './hallucination/verification.js';
20
+ export { completeWithSelfConsistency, selectMostConsistent, textSimilarity, } from './hallucination/consistency.js';
21
+ export { BaseProvider } from './providers/base.js';
22
+ export { OpenAIProvider } from './providers/openai.js';
23
+ export { AnthropicProvider } from './providers/anthropic.js';
24
+ export { GoogleProvider } from './providers/google.js';
25
+ export { OllamaProvider } from './providers/ollama.js';
26
+ export { OpenRouterProvider } from './providers/openrouter.js';
27
+ export { GroqProvider } from './providers/groq.js';
28
+ export { MistralProvider } from './providers/mistral.js';
29
+ export { CohereProvider } from './providers/cohere.js';
30
+ export { Router, FailoverExecutor } from './router/index.js';
31
+ export { SecurityPipeline, NexusSecurityError, SchemaValidator, InjectionDetector, PIIDetector, OutputGuard, InputGuard, SemanticInjectionClassifier, } from './security/index.js';
32
+ export { guardrailPolicy, GUARDRAIL_POLICIES, } from './security/policies.js';
33
+ export { SEMANTIC_INJECTION_CALIBRATION_SET, calibrateSemanticInjectionClassifier, } from './security/injection-calibration.js';
34
+ export { TokenOptimizer, PromptDensifier, BudgetEnforcer, TokenBudgetError, } from './optimizer/index.js';
35
+ export { estimateCost, assertWithinCostBudget, CostBudgetError, } from './optimizer/cost.js';
36
+ export { createOpenAIEmbeddingProvider, createGeminiEmbeddingProvider, createCohereEmbeddingProvider, } from './embeddings/providers.js';
37
+ export { tool, ToolExecutor } from './agent/tool.js';
38
+ export { AgentLoop } from './agent/loop.js';
39
+ export { Tokenizer } from './utils/tokenizer.js';
40
+ export { ingestDocuments, ingestText, } from './rag/ingestion.js';
41
+ export { ingestFilesAfterScan, createPdfExtractor, createOcrExtractor, } from './rag/file-ingestion.js';
42
+ export { UploadScanner, scanUploads, } from './security/upload-scanner.js';
43
+ export { EvalRunner, } from './evals/runner.js';
44
+ export { calculateEvalMetrics, exactMatch, f1Score, semanticSimilarity, passAtK, perplexity, tokensPerSecond, faithfulness, contextualPrecision, contextualRecall, toxicityScore, biasScore, policyAdherence, refusalRate, } from './evals/metrics.js';
45
+ export { summarizeVerifyFormat, ragAnswer, extractStructured, classifyRoute, compareAndDecide, } from './workflow/chains.js';
46
+ export { supportTriageWorkflow, salesQualificationWorkflow, legalReviewWorkflow, codeReviewWorkflow, } from './workflow/domain.js';
47
+ export { runBatch, } from './jobs/batch.js';
48
+ export { JobQueue, } from './jobs/queue.js';
49
+ export { RedisQueueAdapter, BullMQQueueAdapter, } from './jobs/durable-adapters.js';
50
+ export { createFetchUrlTool, createSearchTool, } from './connectors/web.js';
51
+ export { PROVIDER_CONFORMANCE_FIXTURES, runProviderConformance, } from './testing/provider-conformance.js';
52
+ export { OpenTelemetryTraceExporter, } from './ops/otel-tracing.js';
53
+ export { KNOWN_MODELS, resolveProvider } from './types/providers.js';
@@ -0,0 +1,11 @@
1
+ export interface BatchOptions {
2
+ concurrency?: number;
3
+ stopOnError?: boolean;
4
+ }
5
+ export interface BatchItemResult<T> {
6
+ index: number;
7
+ ok: boolean;
8
+ value?: T;
9
+ error?: string;
10
+ }
11
+ export declare function runBatch<TInput, TOutput>(items: TInput[], worker: (item: TInput, index: number) => Promise<TOutput>, options?: BatchOptions): Promise<Array<BatchItemResult<TOutput>>>;
@@ -0,0 +1,32 @@
1
+ export async function runBatch(items, worker, options = {}) {
2
+ const concurrency = Math.max(1, options.concurrency || 3);
3
+ const results = [];
4
+ let nextIndex = 0;
5
+ let stopped = false;
6
+ async function runNext() {
7
+ if (stopped)
8
+ return;
9
+ const index = nextIndex;
10
+ nextIndex += 1;
11
+ if (index >= items.length)
12
+ return;
13
+ try {
14
+ const value = await worker(items[index], index);
15
+ results[index] = { index, ok: true, value };
16
+ }
17
+ catch (error) {
18
+ results[index] = {
19
+ index,
20
+ ok: false,
21
+ error: error instanceof Error ? error.message : String(error),
22
+ };
23
+ if (options.stopOnError) {
24
+ stopped = true;
25
+ return;
26
+ }
27
+ }
28
+ await runNext();
29
+ }
30
+ await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, () => runNext()));
31
+ return results.filter(Boolean);
32
+ }
@@ -0,0 +1,48 @@
1
+ import type { QueueJob } from './queue.js';
2
+ export interface DurableQueueAdapter<TPayload = unknown> {
3
+ enqueue(queueName: string, job: QueueJob<TPayload>): Promise<void> | void;
4
+ get(queueName: string, id: string): Promise<QueueJob<TPayload> | undefined> | QueueJob<TPayload> | undefined;
5
+ update(queueName: string, job: QueueJob<TPayload>): Promise<void> | void;
6
+ list(queueName: string): Promise<Array<QueueJob<TPayload>>> | Array<QueueJob<TPayload>>;
7
+ }
8
+ export interface RedisQueueLikeClient {
9
+ lpush(key: string, value: string): Promise<unknown> | unknown;
10
+ hset(key: string, field: string, value: string): Promise<unknown> | unknown;
11
+ hget(key: string, field: string): Promise<string | null> | string | null;
12
+ hvals(key: string): Promise<string[]> | string[];
13
+ }
14
+ export declare class RedisQueueAdapter<TPayload = unknown> implements DurableQueueAdapter<TPayload> {
15
+ private client;
16
+ private prefix;
17
+ constructor(client: RedisQueueLikeClient, prefix?: string);
18
+ enqueue(queueName: string, job: QueueJob<TPayload>): Promise<void>;
19
+ get(queueName: string, id: string): Promise<QueueJob<TPayload> | undefined>;
20
+ update(queueName: string, job: QueueJob<TPayload>): Promise<void>;
21
+ list(queueName: string): Promise<Array<QueueJob<TPayload>>>;
22
+ private jobsKey;
23
+ private pendingKey;
24
+ }
25
+ export interface BullMQLikeQueue<TPayload = unknown> {
26
+ add(name: string, data: TPayload, options?: Record<string, unknown>): Promise<{
27
+ id?: string | number;
28
+ }> | {
29
+ id?: string | number;
30
+ };
31
+ getJob(id: string): Promise<{
32
+ id: string | number;
33
+ data: TPayload;
34
+ returnvalue?: unknown;
35
+ failedReason?: string;
36
+ } | null>;
37
+ }
38
+ export declare class BullMQQueueAdapter<TPayload = unknown> {
39
+ private queue;
40
+ constructor(queue: BullMQLikeQueue<TPayload>);
41
+ enqueue(name: string, payload: TPayload, options?: Record<string, unknown>): Promise<string>;
42
+ get(id: string): Promise<{
43
+ id: string;
44
+ payload: TPayload;
45
+ result?: unknown;
46
+ error?: string;
47
+ } | undefined>;
48
+ }
@@ -0,0 +1,50 @@
1
+ export class RedisQueueAdapter {
2
+ client;
3
+ prefix;
4
+ constructor(client, prefix = 'nexus-ai-pro:queue:') {
5
+ this.client = client;
6
+ this.prefix = prefix;
7
+ }
8
+ async enqueue(queueName, job) {
9
+ await this.client.hset(this.jobsKey(queueName), job.id, JSON.stringify(job));
10
+ await this.client.lpush(this.pendingKey(queueName), job.id);
11
+ }
12
+ async get(queueName, id) {
13
+ const raw = await this.client.hget(this.jobsKey(queueName), id);
14
+ return raw ? JSON.parse(raw) : undefined;
15
+ }
16
+ async update(queueName, job) {
17
+ await this.client.hset(this.jobsKey(queueName), job.id, JSON.stringify(job));
18
+ }
19
+ async list(queueName) {
20
+ const values = await this.client.hvals(this.jobsKey(queueName));
21
+ return values.map((value) => JSON.parse(value));
22
+ }
23
+ jobsKey(queueName) {
24
+ return `${this.prefix}${queueName}:jobs`;
25
+ }
26
+ pendingKey(queueName) {
27
+ return `${this.prefix}${queueName}:pending`;
28
+ }
29
+ }
30
+ export class BullMQQueueAdapter {
31
+ queue;
32
+ constructor(queue) {
33
+ this.queue = queue;
34
+ }
35
+ async enqueue(name, payload, options = {}) {
36
+ const job = await this.queue.add(name, payload, options);
37
+ return String(job.id || '');
38
+ }
39
+ async get(id) {
40
+ const job = await this.queue.getJob(id);
41
+ if (!job)
42
+ return undefined;
43
+ return {
44
+ id: String(job.id),
45
+ payload: job.data,
46
+ result: job.returnvalue,
47
+ error: job.failedReason,
48
+ };
49
+ }
50
+ }
@@ -0,0 +1,24 @@
1
+ export interface QueueJob<T = unknown> {
2
+ id: string;
3
+ payload: T;
4
+ attempts: number;
5
+ status: 'queued' | 'running' | 'completed' | 'failed';
6
+ result?: unknown;
7
+ error?: string;
8
+ }
9
+ export interface QueueOptions {
10
+ concurrency?: number;
11
+ maxAttempts?: number;
12
+ }
13
+ export declare class JobQueue<TPayload = unknown, TResult = unknown> {
14
+ private worker;
15
+ private options;
16
+ private jobs;
17
+ private running;
18
+ constructor(worker: (payload: TPayload, job: QueueJob<TPayload>) => Promise<TResult>, options?: QueueOptions);
19
+ enqueue(payload: TPayload, id?: string): QueueJob<TPayload>;
20
+ list(): Array<QueueJob<TPayload>>;
21
+ get(id: string): QueueJob<TPayload> | undefined;
22
+ private drain;
23
+ private run;
24
+ }
@@ -0,0 +1,57 @@
1
+ export class JobQueue {
2
+ worker;
3
+ options;
4
+ jobs = [];
5
+ running = 0;
6
+ constructor(worker, options = {}) {
7
+ this.worker = worker;
8
+ this.options = options;
9
+ }
10
+ enqueue(payload, id = `job-${Date.now()}-${Math.random().toString(16).slice(2)}`) {
11
+ const job = {
12
+ id,
13
+ payload,
14
+ attempts: 0,
15
+ status: 'queued',
16
+ };
17
+ this.jobs.push(job);
18
+ void this.drain();
19
+ return job;
20
+ }
21
+ list() {
22
+ return [...this.jobs];
23
+ }
24
+ get(id) {
25
+ return this.jobs.find((job) => job.id === id);
26
+ }
27
+ async drain() {
28
+ const concurrency = this.options.concurrency || 1;
29
+ while (this.running < concurrency) {
30
+ const job = this.jobs.find((item) => item.status === 'queued');
31
+ if (!job)
32
+ return;
33
+ this.running += 1;
34
+ void this.run(job).finally(() => {
35
+ this.running -= 1;
36
+ void this.drain();
37
+ });
38
+ }
39
+ }
40
+ async run(job) {
41
+ job.status = 'running';
42
+ job.attempts += 1;
43
+ try {
44
+ job.result = await this.worker(job.payload, job);
45
+ job.status = 'completed';
46
+ }
47
+ catch (error) {
48
+ job.error = error instanceof Error ? error.message : String(error);
49
+ if (job.attempts < (this.options.maxAttempts || 1)) {
50
+ job.status = 'queued';
51
+ }
52
+ else {
53
+ job.status = 'failed';
54
+ }
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,15 @@
1
+ import type { NexusAIConfig } from '../types/config.js';
2
+ import { type ModelCapabilities } from '../types/providers.js';
3
+ export interface ResolvedModel {
4
+ requestedModel: string;
5
+ model: string;
6
+ providerName: string | null;
7
+ capabilities?: ModelCapabilities;
8
+ }
9
+ export declare function resolveModelAlias(model: string, aliases?: Record<string, string>): string;
10
+ export declare function getModelRegistry(config?: Pick<NexusAIConfig, 'models'>): Record<string, ModelCapabilities>;
11
+ export declare function getModelAliases(config?: Pick<NexusAIConfig, 'models'>): Record<string, string>;
12
+ export declare function resolveModel(model: string, config?: Pick<NexusAIConfig, 'models'>): ResolvedModel;
13
+ export declare function listKnownModels(config?: Pick<NexusAIConfig, 'models'>): string[];
14
+ export declare function listModelsForProvider(providerName: string, config?: Pick<NexusAIConfig, 'models'>): string[];
15
+ export declare function getModelCapabilities(model: string, config?: Pick<NexusAIConfig, 'models'>): ModelCapabilities | undefined;