agentic-flow 1.8.5 → 1.8.7

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.
@@ -0,0 +1,57 @@
1
+ /**
2
+ * TypeScript configuration types for ReasoningBank
3
+ */
4
+
5
+ export interface ReasoningBankConfig {
6
+ retrieve: {
7
+ k: number;
8
+ alpha: number;
9
+ beta: number;
10
+ gamma: number;
11
+ delta: number;
12
+ recency_half_life_days: number;
13
+ min_score: number;
14
+ };
15
+ judge: {
16
+ model: string;
17
+ max_tokens: number;
18
+ confidence_threshold: number;
19
+ };
20
+ distill: {
21
+ model: string;
22
+ max_tokens: number;
23
+ temperature: number;
24
+ };
25
+ consolidate: {
26
+ duplicate_threshold: number;
27
+ contradiction_threshold: number;
28
+ trigger_threshold: number;
29
+ prune_age_days: number;
30
+ prune_min_confidence: number;
31
+ min_confidence_keep: number;
32
+ };
33
+ matts: {
34
+ parallel_k: number;
35
+ sequential_k: number;
36
+ sequential_r: number;
37
+ sequential_stop_on_success: boolean;
38
+ confidence_boost: number;
39
+ };
40
+ embeddings: {
41
+ provider: 'claude' | 'openai';
42
+ model: string;
43
+ dims: number;
44
+ dimensions: number;
45
+ cache_ttl_seconds: number;
46
+ };
47
+ governance: {
48
+ scrub_pii: boolean;
49
+ pii_scrubber: boolean;
50
+ tenant_scoped: boolean;
51
+ };
52
+ features?: {
53
+ enable_pre_task_hook?: boolean;
54
+ enable_post_task_hook?: boolean;
55
+ enable_matts_parallel?: boolean;
56
+ };
57
+ }
@@ -0,0 +1,145 @@
1
+ reasoningbank:
2
+ version: "1.0.0"
3
+ enabled: true
4
+
5
+ # ============================================================================
6
+ # Retrieval Configuration (Algorithm 1)
7
+ # ============================================================================
8
+ retrieve:
9
+ k: 3 # Top-k memories to inject into system prompt
10
+ alpha: 0.65 # Weight: semantic similarity (cosine)
11
+ beta: 0.15 # Weight: recency (exponential decay)
12
+ gamma: 0.20 # Weight: reliability (confidence * usage)
13
+ delta: 0.10 # Weight: diversity penalty (MMR)
14
+ recency_half_life_days: 45 # Exponential decay half-life for age
15
+ duplicate_threshold: 0.87 # Cosine similarity threshold for deduplication
16
+ min_score: 0.3 # Don't inject memories below this score
17
+ max_age_days: 365 # Ignore memories older than this
18
+
19
+ # ============================================================================
20
+ # Embedding Configuration
21
+ # ============================================================================
22
+ embeddings:
23
+ provider: "local" # "claude" | "openai" | "huggingface" | "local"
24
+ model: "Xenova/all-MiniLM-L6-v2" # local transformers.js model
25
+ # model: "claude-sonnet-4-5-20250929" # for Claude provider
26
+ # model: "text-embedding-3-large" # for OpenAI provider
27
+ dimensions: 384 # vector dimensions (local: 384, OpenAI: 1536/3072)
28
+ cache_ttl_seconds: 3600 # cache embeddings for 1 hour
29
+ batch_size: 16 # batch embeddings for efficiency
30
+
31
+ # ============================================================================
32
+ # Judge Configuration (Algorithm 2)
33
+ # ============================================================================
34
+ judge:
35
+ model: "claude-sonnet-4-5-20250929"
36
+ temperature: 0 # deterministic evaluation
37
+ max_tokens: 512
38
+ timeout_ms: 10000
39
+ cache_verdicts: true # cache judgments by trajectory hash
40
+ retry_on_parse_error: true
41
+ fallback_label: "Failure" # conservative fallback on judge error
42
+ fallback_confidence: 0.5
43
+
44
+ # ============================================================================
45
+ # Distillation Configuration (Algorithm 3)
46
+ # ============================================================================
47
+ distill:
48
+ max_items_per_trajectory: 3 # extract up to N memories per trajectory
49
+ success_confidence_prior: 0.75 # initial confidence for success-derived memories
50
+ failure_confidence_prior: 0.60 # initial confidence for failure-derived guardrails
51
+ redact_pii: true # scrub PII before storing
52
+ redact_patterns:
53
+ - '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' # emails
54
+ - '\b(?:\d{3}-\d{2}-\d{4}|\d{9})\b' # SSN
55
+ - '\b(?:sk-[a-zA-Z0-9]{48}|ghp_[a-zA-Z0-9]{36})\b' # API keys (Anthropic, GitHub)
56
+ - '\b(?:xoxb-[a-zA-Z0-9\-]+)\b' # Slack tokens
57
+ - '\b(?:\d{13,19})\b' # Credit card numbers
58
+ min_content_length: 20 # reject memories with content too short
59
+ max_content_length: 2000 # truncate if too long
60
+
61
+ # ============================================================================
62
+ # Consolidation Configuration (Algorithm 4)
63
+ # ============================================================================
64
+ consolidate:
65
+ enabled: true
66
+ run_every_new_items: 20 # consolidate after N new memories
67
+ contradiction_threshold: 0.60 # NLI probability threshold for contradictions
68
+ prune_age_days: 180 # hard delete if unused and old
69
+ min_confidence_keep: 0.30 # prune if confidence drops below this
70
+ max_contradictions_allowed: 5 # quarantine if contradicts > N high-usage items
71
+ dedup_similarity_threshold: 0.87 # merge if cosine > this
72
+ merge_strategy: "keep_highest_usage" # "keep_highest_usage" | "keep_most_recent"
73
+
74
+ # ============================================================================
75
+ # MaTTS Configuration (Algorithm 5)
76
+ # ============================================================================
77
+ matts:
78
+ enabled: true
79
+
80
+ # Parallel mode: k independent rollouts with self-contrast aggregation
81
+ parallel:
82
+ k: 6 # number of parallel rollouts
83
+ diversity_temperature: 0.9 # sampling temperature for diversity
84
+ max_concurrent: 3 # concurrent executions (rate limiting)
85
+ aggregation_model: "claude-sonnet-4-5-20250929"
86
+ aggregation_max_tokens: 2048
87
+
88
+ # Sequential mode: r iterative refinements with check-and-correct
89
+ sequential:
90
+ r: 3 # number of refinement iterations
91
+ check_instruction: "Review your previous attempt. Identify errors or missing steps. Correct and continue."
92
+ max_iterations: 5 # hard cap to prevent infinite loops
93
+ stop_on_success: true # stop if judge labels Success before r iterations
94
+
95
+ # ============================================================================
96
+ # Governance and Compliance
97
+ # ============================================================================
98
+ governance:
99
+ pii_scrubber: true # enable PII redaction
100
+ tenant_scoped: false # set true for multi-tenant deployments
101
+ audit_trail: true # log all memory operations to events table
102
+ max_memory_age_days: 365 # absolute max age before forced deletion
103
+ require_approval_for_high_impact: false # flag high-impact memories for review
104
+
105
+ # ============================================================================
106
+ # Performance and Observability
107
+ # ============================================================================
108
+ performance:
109
+ log_metrics: true # write to performance_metrics table
110
+ export_csv_interval_days: 7 # export metrics CSV weekly
111
+ alert_on_degradation: true # alert if success rate drops
112
+ success_rate_threshold: 0.70 # baseline threshold for alerting
113
+ max_retrieve_latency_ms: 500 # alert if retrieval takes longer
114
+ max_judge_latency_ms: 5000 # alert if judge takes longer
115
+
116
+ # ============================================================================
117
+ # Learning Rate and Confidence Updates
118
+ # ============================================================================
119
+ learning:
120
+ eta: 0.05 # learning rate for confidence updates
121
+ success_boost: 1.0 # confidence += eta * success_boost when used in success
122
+ failure_penalty: -0.5 # confidence += eta * failure_penalty when used in failure
123
+ usage_boost_sigmoid: true # apply sigmoid to usage_count for reliability score
124
+
125
+ # ============================================================================
126
+ # Feature Flags
127
+ # ============================================================================
128
+ features:
129
+ enable_pre_task_hook: true # retrieve and inject memories before task
130
+ enable_post_task_hook: true # judge, distill, consolidate after task
131
+ enable_matts_parallel: true # allow parallel MaTTS
132
+ enable_matts_sequential: true # allow sequential MaTTS
133
+ enable_contradiction_detection: true
134
+ enable_auto_pruning: true
135
+ enable_memory_merging: true
136
+
137
+ # ============================================================================
138
+ # Development and Debugging
139
+ # ============================================================================
140
+ debug:
141
+ verbose_logging: false
142
+ save_trajectories: true # persist all trajectories to task_trajectories
143
+ save_embeddings: true # persist embeddings for inspection
144
+ log_retrieval_scores: false # log detailed scoring breakdown
145
+ dry_run: false # don't actually upsert memories (testing only)
@@ -1,82 +1,109 @@
1
1
  /**
2
2
  * Embedding generation for semantic similarity
3
- * Supports multiple providers: OpenAI, Claude (placeholder), local hashing
3
+ * Uses local transformers.js - no API key required!
4
4
  */
5
+ import { pipeline, env } from '@xenova/transformers';
5
6
  import { loadConfig } from './config.js';
7
+ // Configure transformers.js to use WebAssembly backend (Node.js compatible)
8
+ env.backends.onnx.wasm.numThreads = 1; // Use single thread for stability
9
+ let embeddingPipeline = null;
10
+ let isInitializing = false;
6
11
  const embeddingCache = new Map();
7
12
  /**
8
- * Compute embedding for text
9
- * Uses configured provider (openai, claude, or local)
13
+ * Initialize the embedding pipeline (lazy load)
14
+ */
15
+ async function initializeEmbeddings() {
16
+ if (embeddingPipeline)
17
+ return;
18
+ if (isInitializing) {
19
+ // Wait for initialization to complete
20
+ while (isInitializing) {
21
+ await new Promise(resolve => setTimeout(resolve, 100));
22
+ }
23
+ return;
24
+ }
25
+ isInitializing = true;
26
+ console.log('[Embeddings] Initializing local embedding model (Xenova/all-MiniLM-L6-v2)...');
27
+ console.log('[Embeddings] First run will download ~23MB model...');
28
+ try {
29
+ embeddingPipeline = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', { quantized: true } // Smaller, faster
30
+ );
31
+ console.log('[Embeddings] Local model ready! (384 dimensions)');
32
+ }
33
+ catch (error) {
34
+ console.error('[Embeddings] Failed to initialize:', error?.message || error);
35
+ console.warn('[Embeddings] Falling back to hash-based embeddings');
36
+ }
37
+ finally {
38
+ isInitializing = false;
39
+ }
40
+ }
41
+ /**
42
+ * Compute embedding for text using local model
10
43
  */
11
44
  export async function computeEmbedding(text) {
12
45
  const config = loadConfig();
13
46
  // Check cache
14
- const cacheKey = `${config.embeddings.provider}:${text}`;
47
+ const cacheKey = `local:${text}`;
15
48
  if (embeddingCache.has(cacheKey)) {
16
49
  return embeddingCache.get(cacheKey);
17
50
  }
18
51
  let embedding;
19
- if (config.embeddings.provider === 'openai') {
20
- embedding = await openaiEmbed(text, config.embeddings.model);
21
- }
22
- else if (config.embeddings.provider === 'claude') {
23
- // Claude doesn't have native embeddings yet, use deterministic hash
24
- embedding = hashEmbed(text, config.embeddings.dimensions || 1024);
52
+ // Initialize if needed
53
+ await initializeEmbeddings();
54
+ if (embeddingPipeline) {
55
+ try {
56
+ // Use transformers.js for real embeddings
57
+ const output = await embeddingPipeline(text, {
58
+ pooling: 'mean',
59
+ normalize: true
60
+ });
61
+ embedding = new Float32Array(output.data);
62
+ }
63
+ catch (error) {
64
+ console.error('[Embeddings] Generation failed:', error?.message || error);
65
+ embedding = hashEmbed(text, 384); // Fallback
66
+ }
25
67
  }
26
68
  else {
27
- // Fallback to local hashing
28
- embedding = hashEmbed(text, config.embeddings.dimensions || 1024);
69
+ // Fallback to hash-based embeddings
70
+ const dims = config?.embeddings?.dimensions || 384;
71
+ embedding = hashEmbed(text, dims);
72
+ }
73
+ // Cache with LRU (limit 1000 entries)
74
+ if (embeddingCache.size > 1000) {
75
+ const firstKey = embeddingCache.keys().next().value;
76
+ if (firstKey) {
77
+ embeddingCache.delete(firstKey);
78
+ }
29
79
  }
30
- // Cache with TTL
31
80
  embeddingCache.set(cacheKey, embedding);
32
- setTimeout(() => embeddingCache.delete(cacheKey), config.embeddings.cache_ttl_seconds * 1000);
81
+ // Set TTL for cache entry
82
+ const ttl = config?.embeddings?.cache_ttl_seconds || 3600;
83
+ setTimeout(() => embeddingCache.delete(cacheKey), ttl * 1000);
33
84
  return embedding;
34
85
  }
35
86
  /**
36
- * OpenAI embeddings API
87
+ * Batch compute embeddings (more efficient)
37
88
  */
38
- async function openaiEmbed(text, model) {
39
- const apiKey = process.env.OPENAI_API_KEY;
40
- const config = loadConfig();
41
- if (!apiKey) {
42
- console.warn('[WARN] OPENAI_API_KEY not set, falling back to hash embeddings');
43
- return hashEmbed(text, config.embeddings.dimensions); // Use config dimension
44
- }
45
- try {
46
- const response = await fetch('https://api.openai.com/v1/embeddings', {
47
- method: 'POST',
48
- headers: {
49
- 'Authorization': `Bearer ${apiKey}`,
50
- 'Content-Type': 'application/json'
51
- },
52
- body: JSON.stringify({
53
- model: model || 'text-embedding-3-small',
54
- input: text
55
- })
56
- });
57
- if (!response.ok) {
58
- throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`);
59
- }
60
- const json = await response.json();
61
- return new Float32Array(json.data[0].embedding);
62
- }
63
- catch (error) {
64
- console.error('[ERROR] OpenAI embedding failed:', error);
65
- console.warn('[WARN] Falling back to hash embeddings');
66
- const config = loadConfig();
67
- return hashEmbed(text, config.embeddings.dimensions);
68
- }
89
+ export async function computeEmbeddingBatch(texts) {
90
+ return Promise.all(texts.map(text => computeEmbedding(text)));
91
+ }
92
+ /**
93
+ * Get embedding dimensions
94
+ */
95
+ export function getEmbeddingDimensions() {
96
+ return 384; // all-MiniLM-L6-v2 uses 384 dimensions
69
97
  }
70
98
  /**
71
- * Deterministic hash-based embedding
72
- * For testing and when API keys are unavailable
99
+ * Deterministic hash-based embedding (fallback)
73
100
  */
74
101
  function hashEmbed(text, dims) {
75
102
  const hash = simpleHash(text);
76
103
  const vec = new Float32Array(dims);
77
104
  // Generate deterministic pseudo-random vector from hash
78
105
  for (let i = 0; i < dims; i++) {
79
- vec[i] = Math.sin(hash * (i + 1) * 0.01) * 0.1 + Math.cos(hash * i * 0.02) * 0.05;
106
+ vec[i] = Math.sin(hash * (i + 1) * 0.01) + Math.cos(hash * i * 0.02);
80
107
  }
81
108
  return normalize(vec);
82
109
  }
@@ -87,7 +114,7 @@ function simpleHash(str) {
87
114
  let hash = 0;
88
115
  for (let i = 0; i < str.length; i++) {
89
116
  hash = ((hash << 5) - hash) + str.charCodeAt(i);
90
- hash |= 0; // Convert to 32-bit integer
117
+ hash |= 0;
91
118
  }
92
119
  return Math.abs(hash);
93
120
  }
@@ -108,7 +135,7 @@ function normalize(vec) {
108
135
  return vec;
109
136
  }
110
137
  /**
111
- * Clear embedding cache (for testing)
138
+ * Clear embedding cache
112
139
  */
113
140
  export function clearEmbeddingCache() {
114
141
  embeddingCache.clear();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentic-flow",
3
- "version": "1.8.5",
3
+ "version": "1.8.7",
4
4
  "description": "Production-ready AI agent orchestration platform with 66 specialized agents, 213 MCP tools, ReasoningBank learning memory, and autonomous multi-agent swarms. Built by @ruvnet with Claude Agent SDK, neural networks, memory persistence, GitHub integration, and distributed consensus protocols.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -24,7 +24,7 @@
24
24
  "scripts": {
25
25
  "postinstall": "node scripts/postinstall.js || true",
26
26
  "start": "node --enable-source-maps dist/index.js",
27
- "build": "npm run build:wasm && tsc -p config/tsconfig.json --skipLibCheck || true && cp -r src/reasoningbank/prompts dist/reasoningbank/",
27
+ "build": "npm run build:wasm && tsc -p config/tsconfig.json --skipLibCheck || true && cp -r src/reasoningbank/prompts dist/reasoningbank/ && cp -r src/reasoningbank/config dist/reasoningbank/",
28
28
  "build:wasm": "cd ../reasoningbank && wasm-pack build --target bundler --out-dir pkg/bundler crates/reasoningbank-wasm && wasm-pack build --target web --out-dir pkg/web crates/reasoningbank-wasm && mkdir -p ../agentic-flow/wasm/reasoningbank && cp -r crates/reasoningbank-wasm/pkg/bundler/* ../agentic-flow/wasm/reasoningbank/ && cp -r crates/reasoningbank-wasm/pkg/web ../agentic-flow/wasm/reasoningbank/",
29
29
  "build:wasm:clean": "rm -rf ../reasoningbank/crates/reasoningbank-wasm/pkg && rm -rf wasm/reasoningbank",
30
30
  "dev": "tsx src/index.ts",
@@ -143,6 +143,7 @@
143
143
  "@anthropic-ai/claude-agent-sdk": "^0.1.5",
144
144
  "@anthropic-ai/sdk": "^0.65.0",
145
145
  "@google/genai": "^1.22.0",
146
+ "@xenova/transformers": "^2.17.2",
146
147
  "agentdb": "^1.4.3",
147
148
  "axios": "^1.12.2",
148
149
  "better-sqlite3": "^11.10.0",