feat: vendor midstream and sublinear-time-solver libraries

Add ruvnet/midstream (AIMDS real-time inference) and
ruvnet/sublinear-time-solver (sublinear optimization algorithms)
as vendored dependencies under vendor/.

Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
ruv
2026-03-02 23:32:45 -05:00
parent 14902e6b4e
commit e91bb8a1d5
1600 changed files with 1852646 additions and 0 deletions
+310
View File
@@ -0,0 +1,310 @@
# Lean Agentic Learning System - TypeScript/JavaScript Client
A revolutionary learning framework combining formal reasoning, agentic AI, and stream learning for real-time adaptation.
## Features
- 🎯 **Formal Reasoning** - Lean-style theorem proving for verified knowledge
- 🤖 **Agentic AI** - Autonomous decision-making with Plan-Act-Observe-Learn loops
- 📊 **Stream Learning** - Real-time online adaptation from data streams
- 🧠 **Knowledge Graph** - Dynamic knowledge representation and evolution
-**Real-Time Processing** - Low-latency stream processing
- 🔒 **Type Safety** - Full TypeScript support with comprehensive types
## Installation
```bash
npm install @midstream/lean-agentic
```
## Quick Start
```typescript
import { LeanAgenticClient, StreamProcessor } from '@midstream/lean-agentic';
// Initialize client
const client = new LeanAgenticClient('http://localhost:8080', {
enableFormalVerification: true,
learningRate: 0.01,
maxPlanningDepth: 5,
});
// Create stream processor
const processor = new StreamProcessor(client, 'session_001');
// Process stream chunks
const chunk = {
content: 'Hello, I need weather information',
timestamp: Date.now(),
};
const result = await processor.processChunk(chunk);
console.log('Action:', result.action.description);
console.log('Reward:', result.reward);
console.log('Verified:', result.verified);
```
## Core Concepts
### 1. Agentic Loop (Plan-Act-Observe-Learn)
```typescript
import { AgenticLoop } from '@midstream/lean-agentic';
const loop = new AgenticLoop();
const context = client.createContext('my_session');
// Plan
const plan = await loop.plan(context, 'Get weather for Tokyo');
// The system autonomously:
// - Generates action candidates
// - Ranks by expected reward
// - Executes highest-value action
// - Observes results
// - Learns from experience
```
### 2. Knowledge Graph
```typescript
import { KnowledgeGraph, EntityType } from '@midstream/lean-agentic';
const kg = new KnowledgeGraph();
// Extract entities from text
const entities = kg.extractEntities('Alice works at Google in California');
// Update knowledge graph
kg.update(entities);
// Query entities
const people = kg.queryEntities(EntityType.Person);
const orgs = kg.queryEntities(EntityType.Organization);
// Find related entities
const related = kg.findRelated('alice_entity_id', 2);
```
### 3. Stream Processing
```typescript
import { StreamProcessor } from '@midstream/lean-agentic';
const processor = new StreamProcessor(client, 'session_id');
// Listen to events
processor.on('chunk_processed', ({ chunk, result }) => {
console.log(`Processed: ${chunk.content}`);
console.log(`Reward: ${result.reward}`);
});
processor.on('high_reward', (result) => {
console.log('High reward action detected!', result);
});
// Process stream
const chunks = [
{ content: 'chunk 1', timestamp: Date.now() },
{ content: 'chunk 2', timestamp: Date.now() },
];
const results = await processor.processStream(chunks);
```
### 4. Batched Processing
```typescript
import { BatchedStreamProcessor } from '@midstream/lean-agentic';
// Process in batches of 10
const batchProcessor = new BatchedStreamProcessor(
client,
'session_id',
10
);
batchProcessor.on('batch_processed', ({ result }) => {
console.log('Batch processed with reward:', result.reward);
});
```
## Advanced Usage
### Configuration
```typescript
const client = new LeanAgenticClient('http://localhost:8080', {
// Enable formal verification of all actions
enableFormalVerification: true,
// Learning rate for online adaptation (0.0 - 1.0)
learningRate: 0.01,
// Maximum depth for action planning
maxPlanningDepth: 5,
// Confidence threshold for executing actions
actionThreshold: 0.7,
// Enable multi-agent collaboration
enableMultiAgent: true,
// Knowledge graph update frequency
kgUpdateFreq: 100,
});
```
### Context Management
```typescript
const context = client.createContext('session_001');
// Add to history
context.history.push('Previous message');
// Set preferences
context.preferences['preferred_language'] = 0.9;
context.preferences['detail_level'] = 0.5;
// Update environment
context.environment['user_location'] = 'Tokyo';
context.environment['time_of_day'] = 'morning';
```
### Querying System State
```typescript
// Get system statistics
const stats = await client.getStats();
console.log(`Entities: ${stats.totalEntities}`);
console.log(`Theorems: ${stats.totalTheorems}`);
console.log(`Actions: ${stats.totalActions}`);
console.log(`Avg Reward: ${stats.averageReward}`);
// Get learning statistics
const learningStats = await client.getLearningStats();
console.log(`Iterations: ${learningStats.iterations}`);
console.log(`Parameters: ${learningStats.modelParameters}`);
// Query knowledge graph
const entities = await client.queryEntities({
entityType: 'Person',
searchText: 'Alice',
limit: 10,
});
// Get theorems
const theorems = await client.getTheorems(['safety', 'verified']);
```
## Examples
### Real-time Chat Assistant
```typescript
import { LeanAgenticClient, StreamProcessor } from '@midstream/lean-agentic';
async function chatAssistant() {
const client = new LeanAgenticClient('http://localhost:8080');
const processor = new StreamProcessor(client, 'chat_session');
// Track high-value interactions
processor.on('high_reward', (result) => {
console.log('✨ Learned something valuable!');
});
// Process user messages
const messages = [
'What is the weather like?',
'Remember I prefer detailed forecasts',
'How about tomorrow?',
];
for (const msg of messages) {
const result = await processor.processChunk({
content: msg,
timestamp: Date.now(),
});
console.log(`User: ${msg}`);
console.log(`Action: ${result.action.description}`);
console.log(`Verified: ${result.verified ? '✓' : '✗'}`);
console.log('---');
}
// Get final statistics
const stats = await client.getStats();
console.log('Session Stats:', stats);
}
```
### Learning from Feedback
```typescript
async function learningExample() {
const client = new LeanAgenticClient('http://localhost:8080', {
learningRate: 0.05, // Higher learning rate
});
const context = client.createContext('learning_session');
// Process with feedback loop
for (let i = 0; i < 100; i++) {
const result = await client.processChunk(
`Training example ${i}`,
context
);
// System automatically learns from rewards
// Higher rewards reinforce action selection
}
const stats = await client.getLearningStats();
console.log(`Learned from ${stats.iterations} iterations`);
console.log(`Average reward: ${stats.averageReward}`);
}
```
## API Reference
See [API Documentation](./docs/API.md) for complete reference.
## Architecture
```
┌─────────────────────────────────────────────────────────┐
│ Lean Agentic Learning System │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Formal │ │ Agentic │ │
│ │ Reasoning │◄────►│ Loop │ │
│ │ Engine │ │ (P-A-O-L) │ │
│ └──────┬───────┘ └──────┬───────┘ │
│ │ │ │
│ │ ┌────────────────▼─────┐ │
│ └───►│ Knowledge Graph & │ │
│ │ Theorem Store │ │
│ └────────────┬─────────┘ │
│ │ │
│ ┌────────────▼─────────┐ │
│ │ Stream Learning & │ │
│ │ Online Adaptation │ │
│ └──────────────────────┘ │
└─────────────────────────────────────────────────────────┘
```
## License
MIT
## Contributing
Contributions welcome! See [CONTRIBUTING.md](./CONTRIBUTING.md)
## Support
- Issues: https://github.com/ruvnet/midstream/issues
- Discussions: https://github.com/ruvnet/midstream/discussions
+46
View File
@@ -0,0 +1,46 @@
{
"name": "@midstream/lean-agentic",
"version": "1.0.0",
"description": "TypeScript/JavaScript client for Lean Agentic Learning System - Revolutionary stream learning with formal reasoning and autonomous agents",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "jest",
"lint": "eslint src/**/*.ts",
"prepare": "npm run build"
},
"keywords": [
"lean",
"agentic",
"learning",
"streaming",
"ai",
"agents",
"formal-verification",
"theorem-proving",
"online-learning",
"knowledge-graph",
"llm",
"real-time"
],
"author": "MidStream Contributors",
"license": "MIT",
"dependencies": {
"axios": "^1.6.0",
"ws": "^8.16.0"
},
"devDependencies": {
"@types/node": "^20.11.0",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"typescript": "^5.3.3"
},
"repository": {
"type": "git",
"url": "https://github.com/ruvnet/midstream"
}
}
+121
View File
@@ -0,0 +1,121 @@
/**
* Agentic loop implementation for autonomous decision-making
*/
import {
Action,
Observation,
Plan,
Goal,
Context,
} from './types';
export class AgenticLoop {
private actionHistory: Action[] = [];
private totalReward: number = 0;
private actionCount: number = 0;
/**
* Plan phase: Generate a plan based on goals and context
*/
async plan(context: Context, input: string): Promise<Plan> {
const goal: Goal = {
id: `goal_${this.actionCount}`,
description: `Process: ${input}`,
priority: 1.0,
achieved: false,
};
const actions = await this.generateActionCandidates(input, context);
const rankedActions = this.rankActions(actions, context);
const steps = rankedActions.slice(0, 5).map((action, i) => ({
sequence: i,
action,
preconditions: [],
postconditions: [],
}));
return {
goal,
steps,
estimatedReward: rankedActions[0]?.expectedReward || 0,
confidence: steps.length > 0 ? 0.8 : 0.0,
};
}
/**
* Generate candidate actions based on input
*/
private async generateActionCandidates(
input: string,
context: Context
): Promise<Action[]> {
const candidates: Action[] = [];
const inputLower = input.toLowerCase();
if (inputLower.includes('weather')) {
candidates.push({
actionType: 'get_weather',
description: 'Fetch weather information',
parameters: { query: input },
toolCalls: ['weather_api'],
expectedOutcome: 'Weather data',
expectedReward: 0.8,
});
}
if (inputLower.includes('learn') || inputLower.includes('remember')) {
candidates.push({
actionType: 'update_knowledge',
description: 'Update knowledge graph',
parameters: { content: input },
toolCalls: [],
expectedOutcome: 'Knowledge updated',
expectedReward: 0.9,
});
}
// Default action
candidates.push({
actionType: 'process_text',
description: `Process: ${input}`,
parameters: { text: input },
toolCalls: [],
expectedOutcome: 'Processed text',
expectedReward: 0.5,
});
return candidates;
}
/**
* Rank actions by expected reward
*/
private rankActions(actions: Action[], context: Context): Action[] {
return actions.sort((a, b) => b.expectedReward - a.expectedReward);
}
/**
* Record action execution
*/
recordAction(action: Action, reward: number): void {
this.actionHistory.push(action);
this.totalReward += reward;
this.actionCount++;
}
/**
* Get average reward
*/
getAverageReward(): number {
return this.actionCount > 0 ? this.totalReward / this.actionCount : 0;
}
/**
* Get action count
*/
getActionCount(): number {
return this.actionCount;
}
}
+120
View File
@@ -0,0 +1,120 @@
/**
* Client for interacting with the Lean Agentic Learning System
*/
import axios, { AxiosInstance } from 'axios';
import {
LeanAgenticConfig,
Context,
ProcessingResult,
SystemStats,
Entity,
Theorem,
LearningStats
} from './types';
export class LeanAgenticClient {
private client: AxiosInstance;
private config: LeanAgenticConfig;
constructor(baseURL: string, config: Partial<LeanAgenticConfig> = {}) {
this.client = axios.create({
baseURL,
timeout: 30000,
headers: {
'Content-Type': 'application/json',
},
});
this.config = {
enableFormalVerification: true,
learningRate: 0.01,
maxPlanningDepth: 5,
actionThreshold: 0.7,
enableMultiAgent: true,
kgUpdateFreq: 100,
...config,
};
}
/**
* Process a stream chunk through the lean agentic system
*/
async processChunk(
chunk: string,
context: Context
): Promise<ProcessingResult> {
const response = await this.client.post<ProcessingResult>('/process', {
chunk,
context,
config: this.config,
});
return response.data;
}
/**
* Get system statistics
*/
async getStats(): Promise<SystemStats> {
const response = await this.client.get<SystemStats>('/stats');
return response.data;
}
/**
* Query entities from knowledge graph
*/
async queryEntities(query: {
entityType?: string;
searchText?: string;
limit?: number;
}): Promise<Entity[]> {
const response = await this.client.get<Entity[]>('/knowledge/entities', {
params: query,
});
return response.data;
}
/**
* Get theorems from the formal reasoning system
*/
async getTheorems(tags?: string[]): Promise<Theorem[]> {
const response = await this.client.get<Theorem[]>('/reasoning/theorems', {
params: { tags: tags?.join(',') },
});
return response.data;
}
/**
* Get learning statistics
*/
async getLearningStats(): Promise<LearningStats> {
const response = await this.client.get<LearningStats>('/learning/stats');
return response.data;
}
/**
* Update system configuration
*/
async updateConfig(config: Partial<LeanAgenticConfig>): Promise<void> {
this.config = { ...this.config, ...config };
await this.client.post('/config', this.config);
}
/**
* Create a new context
*/
createContext(sessionId: string): Context {
return {
history: [],
preferences: {},
sessionId,
environment: {},
timestamp: Date.now(),
};
}
}
export default LeanAgenticClient;
+15
View File
@@ -0,0 +1,15 @@
/**
* Lean Agentic Learning System - TypeScript/JavaScript Client
*
* A revolutionary learning framework combining:
* - Formal reasoning (Lean-style theorem proving)
* - Agentic AI (autonomous decision-making)
* - Stream learning (real-time online adaptation)
* - Knowledge evolution (dynamic theorem store)
*/
export * from './types';
export * from './client';
export * from './agent';
export * from './knowledge';
export * from './stream';
+124
View File
@@ -0,0 +1,124 @@
/**
* Knowledge graph for dynamic knowledge representation
*/
import { Entity, EntityType, Relation } from './types';
export class KnowledgeGraph {
private entities: Map<string, Entity> = new Map();
private relations: Relation[] = [];
/**
* Extract entities from text (simple implementation)
*/
extractEntities(text: string): Entity[] {
const entities: Entity[] = [];
const words = text.split(/\s+/);
words.forEach((word, i) => {
// Capitalized words might be entities
if (word.length > 0 && word[0] === word[0].toUpperCase()) {
entities.push({
id: `entity_${i}`,
name: word,
entityType: EntityType.Unknown,
attributes: {},
confidence: 0.7,
});
}
// Numeric values
if (!isNaN(parseFloat(word))) {
entities.push({
id: `value_${i}`,
name: word,
entityType: EntityType.Value,
attributes: {},
confidence: 0.9,
});
}
});
return entities;
}
/**
* Update knowledge graph with new entities
*/
update(entities: Entity[]): void {
entities.forEach(entity => {
const existing = this.entities.get(entity.id);
if (existing) {
// Update existing entity
existing.confidence = (existing.confidence + entity.confidence) / 2;
existing.attributes = { ...existing.attributes, ...entity.attributes };
} else {
// Add new entity
this.entities.set(entity.id, entity);
}
});
}
/**
* Add a relation between entities
*/
addRelation(relation: Relation): void {
this.relations.push(relation);
}
/**
* Query entities by type
*/
queryEntities(entityType?: EntityType): Entity[] {
if (!entityType) {
return Array.from(this.entities.values());
}
return Array.from(this.entities.values()).filter(
e => e.entityType === entityType
);
}
/**
* Find related entities
*/
findRelated(entityId: string, maxDepth: number = 2): Set<string> {
const related = new Set<string>();
const toExplore: Array<[string, number]> = [[entityId, 0]];
while (toExplore.length > 0) {
const [currentId, depth] = toExplore.shift()!;
if (depth >= maxDepth) {
continue;
}
this.relations.forEach(relation => {
if (relation.subject === currentId) {
related.add(relation.object);
toExplore.push([relation.object, depth + 1]);
} else if (relation.object === currentId) {
related.add(relation.subject);
toExplore.push([relation.subject, depth + 1]);
}
});
}
return related;
}
/**
* Get entity count
*/
entityCount(): number {
return this.entities.size;
}
/**
* Get relation count
*/
relationCount(): number {
return this.relations.length;
}
}
+162
View File
@@ -0,0 +1,162 @@
/**
* Stream processing utilities for lean agentic learning
*/
import { EventEmitter } from 'events';
import { ProcessingResult, Context } from './types';
import { LeanAgenticClient } from './client';
export interface StreamChunk {
content: string;
timestamp: number;
metadata?: Record<string, any>;
}
export class StreamProcessor extends EventEmitter {
private client: LeanAgenticClient;
private context: Context;
private chunkBuffer: StreamChunk[] = [];
constructor(client: LeanAgenticClient, sessionId: string) {
super();
this.client = client;
this.context = client.createContext(sessionId);
}
/**
* Process a stream chunk
*/
async processChunk(chunk: StreamChunk): Promise<ProcessingResult> {
this.chunkBuffer.push(chunk);
// Update context
this.context.history.push(chunk.content);
this.context.timestamp = chunk.timestamp;
// Process through lean agentic system
const result = await this.client.processChunk(
chunk.content,
this.context
);
// Emit events
this.emit('chunk_processed', { chunk, result });
if (result.reward > 0.8) {
this.emit('high_reward', result);
}
return result;
}
/**
* Process multiple chunks
*/
async processStream(chunks: StreamChunk[]): Promise<ProcessingResult[]> {
const results: ProcessingResult[] = [];
for (const chunk of chunks) {
const result = await this.processChunk(chunk);
results.push(result);
}
this.emit('stream_complete', { results });
return results;
}
/**
* Get current context
*/
getContext(): Context {
return { ...this.context };
}
/**
* Update context preferences
*/
updatePreference(key: string, value: number): void {
this.context.preferences[key] = value;
}
/**
* Clear buffer
*/
clearBuffer(): void {
this.chunkBuffer = [];
}
/**
* Get buffer size
*/
getBufferSize(): number {
return this.chunkBuffer.length;
}
}
/**
* Create a stream from an async iterator
*/
export async function* streamFromAsyncIterator<T>(
iterator: AsyncIterableIterator<T>
): AsyncGenerator<StreamChunk> {
for await (const item of iterator) {
yield {
content: String(item),
timestamp: Date.now(),
};
}
}
/**
* Create a batched stream processor
*/
export class BatchedStreamProcessor extends StreamProcessor {
private batchSize: number;
private currentBatch: StreamChunk[] = [];
constructor(client: LeanAgenticClient, sessionId: string, batchSize: number = 10) {
super(client, sessionId);
this.batchSize = batchSize;
}
async processChunk(chunk: StreamChunk): Promise<ProcessingResult> {
this.currentBatch.push(chunk);
if (this.currentBatch.length >= this.batchSize) {
return this.processBatch();
}
// Return a pending result
return {
action: {
actionType: 'buffer',
description: 'Buffering chunk',
parameters: {},
toolCalls: [],
expectedReward: 0,
},
observation: {
success: true,
result: 'Buffered',
changes: [],
timestamp: Date.now(),
},
reward: 0,
verified: false,
};
}
private async processBatch(): Promise<ProcessingResult> {
const combined = this.currentBatch.map(c => c.content).join(' ');
const result = await super.processChunk({
content: combined,
timestamp: Date.now(),
});
this.currentBatch = [];
this.emit('batch_processed', { result });
return result;
}
}
+256
View File
@@ -0,0 +1,256 @@
/**
* Core types for the Lean Agentic Learning System
*/
export interface LeanAgenticConfig {
/** Enable formal verification of actions */
enableFormalVerification: boolean;
/** Learning rate for online adaptation */
learningRate: number;
/** Maximum planning depth */
maxPlanningDepth: number;
/** Confidence threshold for action execution */
actionThreshold: number;
/** Enable multi-agent collaboration */
enableMultiAgent: boolean;
/** Knowledge graph update frequency */
kgUpdateFreq: number;
}
export interface Context {
/** Conversation history */
history: string[];
/** User preferences learned over time */
preferences: Record<string, number>;
/** Session identifier */
sessionId: string;
/** Environment state */
environment: Record<string, any>;
/** Timestamp */
timestamp: number;
}
export interface Action {
/** Type of action */
actionType: string;
/** Human-readable description */
description: string;
/** Action parameters */
parameters: Record<string, string>;
/** Tool calls required */
toolCalls: string[];
/** Expected outcome */
expectedOutcome?: string;
/** Expected reward */
expectedReward: number;
}
export interface Observation {
/** Whether action succeeded */
success: boolean;
/** Result of action */
result: string;
/** Changes made */
changes: string[];
/** Timestamp */
timestamp: number;
}
export interface Plan {
/** Goal being pursued */
goal: Goal;
/** Steps to achieve goal */
steps: PlanStep[];
/** Estimated total reward */
estimatedReward: number;
/** Confidence in plan */
confidence: number;
}
export interface PlanStep {
/** Step sequence number */
sequence: number;
/** Action to take */
action: Action;
/** Preconditions for this step */
preconditions: string[];
/** Postconditions after this step */
postconditions: string[];
}
export interface Goal {
/** Goal identifier */
id: string;
/** Description */
description: string;
/** Priority (0-1) */
priority: number;
/** Whether achieved */
achieved: boolean;
}
export interface Theorem {
/** Theorem identifier */
id: string;
/** Mathematical statement */
statement: string;
/** Proof (if proven) */
proof?: Proof;
/** Confidence score */
confidence: number;
/** Tags for categorization */
tags: string[];
}
export interface Proof {
/** Proof steps */
steps: ProofStep[];
/** Whether proof is valid */
valid: boolean;
/** Overall confidence */
confidence: number;
}
export interface ProofStep {
/** Inference rule used */
rule: string;
/** Premises */
premises: string[];
/** Conclusion */
conclusion: string;
/** Step confidence */
confidence: number;
}
export interface Entity {
/** Entity identifier */
id: string;
/** Entity name */
name: string;
/** Entity type */
entityType: EntityType;
/** Attributes */
attributes: Record<string, string>;
/** Confidence score */
confidence: number;
}
export enum EntityType {
Person = "Person",
Place = "Place",
Organization = "Organization",
Concept = "Concept",
Event = "Event",
Value = "Value",
Unknown = "Unknown"
}
export interface Relation {
/** Relation identifier */
id: string;
/** Subject entity */
subject: string;
/** Predicate/relation type */
predicate: string;
/** Object entity */
object: string;
/** Confidence score */
confidence: number;
/** Source of relation */
source: string;
}
export interface ProcessingResult {
/** Action taken */
action: Action;
/** Observation received */
observation: Observation;
/** Reward earned */
reward: number;
/** Whether formally verified */
verified: boolean;
}
export interface SystemStats {
/** Total theorems in knowledge base */
totalTheorems: number;
/** Total entities in knowledge graph */
totalEntities: number;
/** Learning iterations completed */
learningIterations: number;
/** Total actions executed */
totalActions: number;
/** Average reward */
averageReward: number;
}
export enum AdaptationStrategy {
Immediate = "Immediate",
Batched = "Batched",
ExperienceReplay = "ExperienceReplay"
}
export interface LearningStats {
/** Total iterations */
iterations: number;
/** Experience buffer size */
bufferSize: number;
/** Average reward */
averageReward: number;
/** Model parameter count */
modelParameters: number;
}
+20
View File
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"outDir": "./dist",
"rootDir": "./src",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"moduleResolution": "node"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
}