Rename LLM service to Intent service and refactor relevant code
Refactored the `DummyLlmService` to `DummyIntentService` across the codebase for clarity and consistency. Updated variable names, method calls, and documentation to align with this change. This improves code readability and better reflects the service's purpose.
This commit is contained in:
302
README.md
302
README.md
@@ -1,10 +1,9 @@
|
|||||||
# workflow-function-manifold
|
# workflow-function-manifold
|
||||||
|
|
||||||
> A TypeScript/JavaScript library for building dynamic, LLM-driven workflows
|
> A TypeScript/JavaScript library for building dynamic, LLM-driven workflows using a region-based execution model.
|
||||||
> using a region-based execution model
|
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
CLI: `npx workflow-function-manifold`
|
CLI: `npx workflow-function-manifold`
|
||||||
|
|
||||||
@@ -27,7 +26,7 @@ CLI: `npx workflow-function-manifold`
|
|||||||
`workflow-function-manifold` enables you to create dynamic workflows that:
|
`workflow-function-manifold` enables you to create dynamic workflows that:
|
||||||
|
|
||||||
- Navigate between different execution regions based on LLM-interpreted intents.
|
- Navigate between different execution regions based on LLM-interpreted intents.
|
||||||
- Execute operations within regions based on state and context.
|
- Execute operations within regions using state and context.
|
||||||
- Maintain workflow state across operations.
|
- Maintain workflow state across operations.
|
||||||
- Support flexible region-to-region connections.
|
- Support flexible region-to-region connections.
|
||||||
|
|
||||||
@@ -36,7 +35,7 @@ graph TD
|
|||||||
MF[Workflow Function Manifold] -->|Initialize| CR[Current Region]
|
MF[Workflow Function Manifold] -->|Initialize| CR[Current Region]
|
||||||
MF -->|Navigate| AR[Adjacent Regions]
|
MF -->|Navigate| AR[Adjacent Regions]
|
||||||
MF -->|Execute| OP[Operators]
|
MF -->|Execute| OP[Operators]
|
||||||
OP -->|Use| LLM[LLM Service]
|
OP -->|Use| Intent[Intent Service]
|
||||||
LLM -->|Match| INT[Intent]
|
LLM -->|Match| INT[Intent]
|
||||||
INT --> OP
|
INT --> OP
|
||||||
OP -->|Update| ST[Workflow State]
|
OP -->|Update| ST[Workflow State]
|
||||||
@@ -45,54 +44,21 @@ graph TD
|
|||||||
NR -->|If Valid| CR
|
NR -->|If Valid| CR
|
||||||
CR -->|Continue| OP
|
CR -->|Continue| OP
|
||||||
NR -->|If Invalid| END[End]
|
NR -->|If Invalid| END[End]
|
||||||
|
|
||||||
style MF fill:#000000,stroke:#FFFFFF,stroke-width:4px,color:#ffffff
|
style MF fill:#000000,stroke:#FFFFFF,stroke-width:4px,color:#ffffff
|
||||||
style CR fill:#222222,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
|
|
||||||
style AR fill:#222222,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
|
|
||||||
style OP fill:#333333,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
|
|
||||||
style LLM fill:#333333,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
|
|
||||||
style INT fill:#333333,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
|
|
||||||
style NR fill:#333333,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
|
|
||||||
style END fill:#222222,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
|
|
||||||
style ST fill:#444444,stroke:#FFFFFF,stroke-width:2px,color:#ffffff
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
Use as a library:
|
|
||||||
|
Install the library via npm:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install workflow-function-manifold
|
npm install workflow-function-manifold
|
||||||
```
|
```
|
||||||
For a very basic demonstration, invoke the CLI via npx:
|
|
||||||
```shell
|
|
||||||
$ npx workflow-function-manifold
|
|
||||||
|
|
||||||
🔄 Executing Workflow...
|
Run a basic demonstration using the CLI:
|
||||||
|
|
||||||
📍 Step: Nested: Data Validation
|
|
||||||
Prompt: "validate the input"
|
|
||||||
✓ Validating data structure
|
|
||||||
✅ Execution complete
|
|
||||||
|
|
||||||
📍 Step: Nested: Data Cleaning
|
|
||||||
Prompt: "clean the data"
|
|
||||||
↪ Navigation successful
|
|
||||||
✓ Cleaning data
|
|
||||||
✅ Execution complete
|
|
||||||
|
|
||||||
📍 Step: Main: Data Analysis
|
|
||||||
Prompt: "analyze the results"
|
|
||||||
↪ Navigation successful
|
|
||||||
✓ Performing data analysis
|
|
||||||
✅ Execution complete
|
|
||||||
|
|
||||||
📍 Step: Main: Data Transformation
|
|
||||||
Prompt: "transform the output"
|
|
||||||
↪ Navigation successful
|
|
||||||
✓ Transforming results
|
|
||||||
✅ Execution complete
|
|
||||||
|
|
||||||
🎉 Workflow Demonstration Complete!
|
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npx workflow-function-manifold
|
||||||
```
|
```
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
@@ -102,147 +68,91 @@ import {
|
|||||||
WorkflowFunctionManifold,
|
WorkflowFunctionManifold,
|
||||||
ManifoldRegion,
|
ManifoldRegion,
|
||||||
WorkflowOperator,
|
WorkflowOperator,
|
||||||
DummyLlmService,
|
DummyIntentService,
|
||||||
} from 'workflow-function-manifold';
|
} from 'workflow-function-manifold';
|
||||||
|
|
||||||
// Initialize the manifold with an LLM service
|
const llm = new DummyIntentService();
|
||||||
const llm = new DummyLlmService();
|
|
||||||
const manifold = new WorkflowFunctionManifold(llm);
|
const manifold = new WorkflowFunctionManifold(llm);
|
||||||
|
|
||||||
// Create an operator
|
const analysisOperator = new WorkflowOperator('analysis', async state => ({
|
||||||
const analysisOperator = new WorkflowOperator('analysis', async state => {
|
...state,
|
||||||
console.log('Analyzing data...');
|
analyzed: true,
|
||||||
return { ...state, analyzed: true };
|
}));
|
||||||
});
|
|
||||||
|
|
||||||
// Create and connect regions
|
|
||||||
const analysisRegion = new ManifoldRegion('analysis', [analysisOperator]);
|
const analysisRegion = new ManifoldRegion('analysis', [analysisOperator]);
|
||||||
|
|
||||||
manifold.addRegion(analysisRegion);
|
manifold.addRegion(analysisRegion);
|
||||||
|
|
||||||
// Execute workflow
|
await manifold.navigate('analyze the data');
|
||||||
await manifold.navigate('analyze the data'); // This navigates to the 'analysis' region
|
await manifold.executeWorkflow('analyze the data');
|
||||||
await manifold.executeWorkflow('analyze the data'); // Executes the operator in the 'analysis' region
|
|
||||||
|
|
||||||
console.log(manifold.state); // { analyzed: true }
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> **Note:** The `DummyLlmService` matches specific keywords in prompts. Ensure
|
> **Note:** `DummyIntentService` uses basic keyword matching. Include keywords like `'analyze'`, `'process'`, or `'transform'` for default operators to work.
|
||||||
> your prompts contain keywords like `'analyze'`, `'process'`, or `'transform'`
|
|
||||||
> for the default operators to function.
|
|
||||||
|
|
||||||
## Core Components
|
## Core Components
|
||||||
|
|
||||||
### WorkflowFunctionManifold
|
### `WorkflowFunctionManifold`
|
||||||
|
|
||||||
The main class that orchestrates workflow execution.
|
The main orchestrator for workflow execution.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const manifold = new WorkflowFunctionManifold(llmService);
|
const manifold = new WorkflowFunctionManifold(llmService);
|
||||||
|
|
||||||
// Add regions
|
|
||||||
manifold.addRegion(region);
|
manifold.addRegion(region);
|
||||||
|
|
||||||
// Navigate between regions
|
|
||||||
await manifold.navigate(prompt);
|
await manifold.navigate(prompt);
|
||||||
|
|
||||||
// Execute operations
|
|
||||||
await manifold.executeWorkflow(prompt);
|
await manifold.executeWorkflow(prompt);
|
||||||
```
|
```
|
||||||
|
|
||||||
### ManifoldRegion
|
### `ManifoldRegion`
|
||||||
|
|
||||||
Represents a workflow region containing operators and connections to other
|
Represents a workflow region containing operators and connections to other regions.
|
||||||
regions.
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const region = new ManifoldRegion('regionName', [operator1, operator2]);
|
const region = new ManifoldRegion('regionName', [operator1, operator2]);
|
||||||
|
|
||||||
// Connect regions
|
|
||||||
region.connectTo(otherRegion);
|
region.connectTo(otherRegion);
|
||||||
|
|
||||||
// Add operators
|
|
||||||
region.addOperator(newOperator);
|
region.addOperator(newOperator);
|
||||||
```
|
```
|
||||||
|
|
||||||
### WorkflowOperator
|
### `WorkflowOperator`
|
||||||
|
|
||||||
Defines operations that can be executed within regions.
|
Defines an operation that can be executed within a region.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const operator = new WorkflowOperator('operatorName', async state => {
|
const operator = new WorkflowOperator('operatorName', async state => newState);
|
||||||
// Modify state
|
|
||||||
return newState;
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### DummyLlmService
|
### `DummyIntentService`
|
||||||
|
|
||||||
A simple LLM service simulation for intent matching.
|
A basic intent-matching service.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const llm = new DummyLlmService();
|
const intentService = new DummyIntentService();
|
||||||
const intent = await llm.query('analyze the data');
|
const intent = await intentService.query('analyze the data');
|
||||||
// Returns: { confidence: 0.9, action: 'analysis' }
|
|
||||||
```
|
|
||||||
|
|
||||||
## Nested Manifolds
|
|
||||||
|
|
||||||
### NestedManifoldRegion
|
|
||||||
|
|
||||||
A `NestedManifoldRegion` allows embedding a `WorkflowFunctionManifold` inside a region. This enables creating hierarchical workflows where regions themselves contain sub-workflows.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import {
|
|
||||||
WorkflowFunctionManifold,
|
|
||||||
NestedManifoldRegion,
|
|
||||||
ManifoldRegion,
|
|
||||||
WorkflowOperator,
|
|
||||||
DummyLlmService,
|
|
||||||
} from 'workflow-function-manifold';
|
|
||||||
|
|
||||||
// Sub-workflow
|
|
||||||
const nestedLlm = new DummyLlmService();
|
|
||||||
const nestedManifold = new WorkflowFunctionManifold(nestedLlm);
|
|
||||||
|
|
||||||
const nestedOperator = new WorkflowOperator('nestedOperation', async state => {
|
|
||||||
return { ...state, nested: true };
|
|
||||||
});
|
|
||||||
|
|
||||||
const nestedRegion = new ManifoldRegion('nestedRegion', [nestedOperator]);
|
|
||||||
nestedManifold.addRegion(nestedRegion);
|
|
||||||
|
|
||||||
// Main workflow
|
|
||||||
const mainLlm = new DummyLlmService();
|
|
||||||
const mainManifold = new WorkflowFunctionManifold(mainLlm);
|
|
||||||
|
|
||||||
const nestedManifoldRegion = new NestedManifoldRegion('nestedManifoldRegion', nestedManifold);
|
|
||||||
mainManifold.addRegion(nestedManifoldRegion);
|
|
||||||
|
|
||||||
await mainManifold.navigate('perform nested operation');
|
|
||||||
await mainManifold.executeWorkflow('perform nested operation');
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Complete Example
|
## Complete Example
|
||||||
|
|
||||||
Here's a full example demonstrating a three-stage workflow:
|
Here's a complete workflow demonstration:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
async function createWorkflow() {
|
async function createWorkflow() {
|
||||||
const llm = new DummyLlmService();
|
const intentService = new DummyIntentService();
|
||||||
const manifold = new WorkflowFunctionManifold(llm);
|
const manifold = new WorkflowFunctionManifold(intentService);
|
||||||
|
|
||||||
// Create operators
|
const analysisOp = new WorkflowOperator('analysis', async state => ({
|
||||||
const analysisOp = new WorkflowOperator('analysis', async state => {
|
...state,
|
||||||
return { ...state, analyzed: true };
|
analyzed: true,
|
||||||
});
|
}));
|
||||||
const processingOp = new WorkflowOperator('processing', async state => {
|
|
||||||
return { ...state, processed: true };
|
const processingOp = new WorkflowOperator('processing', async state => ({
|
||||||
});
|
...state,
|
||||||
const transformOp = new WorkflowOperator('transformation', async state => {
|
processed: true,
|
||||||
return { ...state, transformed: true };
|
}));
|
||||||
});
|
|
||||||
|
const transformOp = new WorkflowOperator('transformation', async state => ({
|
||||||
|
...state,
|
||||||
|
transformed: true,
|
||||||
|
}));
|
||||||
|
|
||||||
// Create and connect regions
|
|
||||||
const analysisRegion = new ManifoldRegion('analysis', [analysisOp]);
|
const analysisRegion = new ManifoldRegion('analysis', [analysisOp]);
|
||||||
const processingRegion = new ManifoldRegion('processing', [processingOp]);
|
const processingRegion = new ManifoldRegion('processing', [processingOp]);
|
||||||
const transformRegion = new ManifoldRegion('transformation', [transformOp]);
|
const transformRegion = new ManifoldRegion('transformation', [transformOp]);
|
||||||
@@ -250,7 +160,6 @@ async function createWorkflow() {
|
|||||||
analysisRegion.connectTo(processingRegion);
|
analysisRegion.connectTo(processingRegion);
|
||||||
processingRegion.connectTo(transformRegion);
|
processingRegion.connectTo(transformRegion);
|
||||||
|
|
||||||
// Add regions to manifold
|
|
||||||
manifold.addRegion(analysisRegion);
|
manifold.addRegion(analysisRegion);
|
||||||
manifold.addRegion(processingRegion);
|
manifold.addRegion(processingRegion);
|
||||||
manifold.addRegion(transformRegion);
|
manifold.addRegion(transformRegion);
|
||||||
@@ -258,37 +167,31 @@ async function createWorkflow() {
|
|||||||
return manifold;
|
return manifold;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute workflow
|
|
||||||
const manifold = await createWorkflow();
|
const manifold = await createWorkflow();
|
||||||
const prompts = [
|
|
||||||
'analyze the data',
|
const prompts = ['analyze the data', 'process the results', 'transform the output'];
|
||||||
'process the results',
|
|
||||||
'transform the output',
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const prompt of prompts) {
|
for (const prompt of prompts) {
|
||||||
await manifold.navigate(prompt);
|
await manifold.navigate(prompt);
|
||||||
await manifold.executeWorkflow(prompt);
|
await manifold.executeWorkflow(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(manifold.state); // Final state after all operations
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## API Reference
|
## API Reference
|
||||||
|
|
||||||
### WorkflowFunctionManifold
|
### `WorkflowFunctionManifold`
|
||||||
|
|
||||||
#### Constructor
|
#### Constructor
|
||||||
|
|
||||||
- `constructor(llmService: LLMService)`
|
- `constructor(intentService: IntentService)`
|
||||||
|
|
||||||
#### Methods
|
#### Methods
|
||||||
|
|
||||||
- `addRegion(region: ManifoldRegion): void`
|
- `addRegion(region: ManifoldRegion | NestedManifoldRegion): void`
|
||||||
- `async navigate(prompt: string): Promise<boolean>`
|
- `navigate(prompt: string): Promise<boolean>`
|
||||||
- `async executeWorkflow(prompt: string): Promise<boolean>`
|
- `executeWorkflow(prompt: string): Promise<boolean>`
|
||||||
|
|
||||||
### ManifoldRegion
|
### `ManifoldRegion`
|
||||||
|
|
||||||
#### Constructor
|
#### Constructor
|
||||||
|
|
||||||
@@ -298,104 +201,37 @@ console.log(manifold.state); // Final state after all operations
|
|||||||
|
|
||||||
- `addOperator(operator: WorkflowOperator): void`
|
- `addOperator(operator: WorkflowOperator): void`
|
||||||
- `connectTo(region: ManifoldRegion): void`
|
- `connectTo(region: ManifoldRegion): void`
|
||||||
- `async getValidOperators(state: any): Promise<WorkflowOperator[]>`
|
|
||||||
|
|
||||||
### WorkflowOperator
|
|
||||||
|
|
||||||
#### Constructor
|
|
||||||
|
|
||||||
- `constructor(name: string, operation: (state: any) => Promise<any>)`
|
|
||||||
|
|
||||||
#### Methods
|
|
||||||
|
|
||||||
- `async execute(state: any): Promise<any>`
|
|
||||||
|
|
||||||
### NestedManifoldRegion
|
|
||||||
- Extends `ManifoldRegion` and embeds a `WorkflowFunctionManifold`.
|
|
||||||
|
|
||||||
|
|
||||||
## State Management
|
## State Management
|
||||||
|
|
||||||
The workflow maintains state across operations. Each operator can access and
|
Operators access and modify the state persistently:
|
||||||
modify the state:
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const operator = new WorkflowOperator('example', async state => {
|
const operator = new WorkflowOperator('example', async state => ({
|
||||||
// Access existing state
|
...state,
|
||||||
const { previousValue } = state;
|
newValue: 'updated',
|
||||||
|
}));
|
||||||
// Return modified state
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
newValue: 'updated',
|
|
||||||
};
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
The updated state persists across operators and regions.
|
|
||||||
|
|
||||||
## LLM Integration
|
|
||||||
|
|
||||||
The system uses LLM services for intent recognition. The default
|
|
||||||
`DummyLlmService` provides basic intent matching, but you can implement your own
|
|
||||||
LLM service:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
class CustomLLMService {
|
|
||||||
async query(prompt) {
|
|
||||||
// Implement custom LLM logic
|
|
||||||
return {
|
|
||||||
confidence: number,
|
|
||||||
action: string,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Error Handling
|
## Error Handling
|
||||||
|
|
||||||
This library includes basic error handling to ensure workflows run smoothly,
|
|
||||||
even when unexpected issues arise.
|
|
||||||
|
|
||||||
### Navigation Errors
|
### Navigation Errors
|
||||||
|
|
||||||
- If a prompt doesn't match a valid adjacent region:
|
- Logs warnings for unmatched prompts.
|
||||||
- Logs a warning: `No valid region found for prompt: "<prompt>"`.
|
|
||||||
|
|
||||||
### Operator Execution Errors
|
### Operator Execution Errors
|
||||||
|
|
||||||
- If no matching operator is found:
|
- Logs warnings for unmatched operators.
|
||||||
- Logs a warning: `No matching operator found for intent: <intent>`.
|
|
||||||
|
|
||||||
### LLM Query Errors
|
---
|
||||||
|
|
||||||
- If issues arise during LLM queries:
|
|
||||||
- Logs an error:
|
|
||||||
`Error during navigation for prompt "<prompt>": <error message>`.
|
|
||||||
|
|
||||||
### Example Error Logging
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
try {
|
|
||||||
await manifold.navigate('unknown command');
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Critical navigation error:', error);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await manifold.executeWorkflow('perform unknown action');
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Critical execution error:', error);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
1. Fork the repository.
|
1. Fork the repository.
|
||||||
2. Create your feature branch: `git checkout -b feature/my-feature`.
|
2. Create a new branch: `git checkout -b feature/my-feature`.
|
||||||
3. Commit your changes: `git commit -am 'Add new feature'`.
|
3. Commit changes: `git commit -m "Add my feature"`.
|
||||||
4. Push to the branch: `git push origin feature/my-feature`.
|
4. Push the branch: `git push origin feature/my-feature`.
|
||||||
5. Submit a pull request.
|
5. Open a pull request.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
41
bin.js
41
bin.js
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import {
|
import {
|
||||||
DummyLlmService,
|
DummyIntentService,
|
||||||
ManifoldRegion,
|
ManifoldRegion,
|
||||||
WorkflowFunctionManifold,
|
WorkflowFunctionManifold,
|
||||||
WorkflowOperator,
|
WorkflowOperator,
|
||||||
@@ -9,45 +9,33 @@ import {
|
|||||||
} from './lib.js';
|
} from './lib.js';
|
||||||
|
|
||||||
async function demonstrateNestedManifold() {
|
async function demonstrateNestedManifold() {
|
||||||
console.log('\n🚀 Starting Nested Manifold Demonstration\n');
|
const nestedIntentService = new DummyIntentService();
|
||||||
|
const nestedManifold = new WorkflowFunctionManifold(nestedIntentService);
|
||||||
console.log('📦 Creating Secondary Manifold...');
|
|
||||||
const nestedLlm = new DummyLlmService();
|
|
||||||
const nestedManifold = new WorkflowFunctionManifold(nestedLlm);
|
|
||||||
|
|
||||||
const validateOp = new WorkflowOperator('validation', async state => {
|
const validateOp = new WorkflowOperator('validation', async state => {
|
||||||
console.log(' ✓ Validating data structure');
|
|
||||||
return { ...state, validated: true };
|
return { ...state, validated: true };
|
||||||
});
|
});
|
||||||
|
|
||||||
const cleanOp = new WorkflowOperator('cleaning', async state => {
|
const cleanOp = new WorkflowOperator('cleaning', async state => {
|
||||||
console.log(' ✓ Cleaning data');
|
|
||||||
return { ...state, cleaned: true };
|
return { ...state, cleaned: true };
|
||||||
});
|
});
|
||||||
|
|
||||||
const validateRegion = new ManifoldRegion('validation', [validateOp]);
|
const validateRegion = new ManifoldRegion('validation', [validateOp]);
|
||||||
const cleanRegion = new ManifoldRegion('cleaning', [cleanOp]);
|
const cleanRegion = new ManifoldRegion('cleaning', [cleanOp]);
|
||||||
|
|
||||||
// Set up nested manifold regions
|
|
||||||
validateRegion.connectTo(cleanRegion);
|
validateRegion.connectTo(cleanRegion);
|
||||||
nestedManifold.addRegion(validateRegion);
|
nestedManifold.addRegion(validateRegion);
|
||||||
nestedManifold.addRegion(cleanRegion);
|
nestedManifold.addRegion(cleanRegion);
|
||||||
|
|
||||||
console.log('📦 Creating Primary Manifold...');
|
const mainIntentService = new DummyIntentService();
|
||||||
const mainLlm = new DummyLlmService();
|
const mainManifold = new WorkflowFunctionManifold(mainIntentService);
|
||||||
const mainManifold = new WorkflowFunctionManifold(mainLlm);
|
|
||||||
|
|
||||||
const analysisOp = new WorkflowOperator('analysis', async state => {
|
const analysisOp = new WorkflowOperator('analysis', async state => {
|
||||||
console.log(' ✓ Performing data analysis');
|
|
||||||
return { ...state, analyzed: true };
|
return { ...state, analyzed: true };
|
||||||
});
|
});
|
||||||
|
|
||||||
const transformOp = new WorkflowOperator('transformation', async state => {
|
const transformOp = new WorkflowOperator('transformation', async state => {
|
||||||
console.log(' ✓ Transforming results');
|
|
||||||
return { ...state, transformed: true };
|
return { ...state, transformed: true };
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set up main manifold regions
|
|
||||||
const nestedPreprocessRegion = new NestedManifoldRegion('preprocessing', nestedManifold);
|
const nestedPreprocessRegion = new NestedManifoldRegion('preprocessing', nestedManifold);
|
||||||
const analysisRegion = new ManifoldRegion('analysis', [analysisOp]);
|
const analysisRegion = new ManifoldRegion('analysis', [analysisOp]);
|
||||||
const transformRegion = new ManifoldRegion('transformation', [transformOp]);
|
const transformRegion = new ManifoldRegion('transformation', [transformOp]);
|
||||||
@@ -59,8 +47,6 @@ async function demonstrateNestedManifold() {
|
|||||||
mainManifold.addRegion(analysisRegion);
|
mainManifold.addRegion(analysisRegion);
|
||||||
mainManifold.addRegion(transformRegion);
|
mainManifold.addRegion(transformRegion);
|
||||||
|
|
||||||
console.log('\n🔄 Executing Workflow...\n');
|
|
||||||
|
|
||||||
const prompts = [
|
const prompts = [
|
||||||
{ text: 'validate the input', description: 'Nested: Data Validation' },
|
{ text: 'validate the input', description: 'Nested: Data Validation' },
|
||||||
{ text: 'clean the data', description: 'Nested: Data Cleaning' },
|
{ text: 'clean the data', description: 'Nested: Data Cleaning' },
|
||||||
@@ -69,31 +55,24 @@ async function demonstrateNestedManifold() {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (const { text, description } of prompts) {
|
for (const { text, description } of prompts) {
|
||||||
console.log(`📍 Step: ${description}\n Prompt: "${text}"`);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// First try to navigate
|
|
||||||
const navigated = await mainManifold.navigate(text);
|
const navigated = await mainManifold.navigate(text);
|
||||||
if (navigated) {
|
if (navigated) {
|
||||||
console.log(' ↪ Navigation successful');
|
console.log(`📍 Step: ${description}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then execute the workflow
|
|
||||||
const executed = await mainManifold.executeWorkflow(text);
|
const executed = await mainManifold.executeWorkflow(text);
|
||||||
if (executed) {
|
if (executed) {
|
||||||
console.log(' ✅ Execution complete\n');
|
console.log(`✅ Execution complete`);
|
||||||
} else {
|
} else {
|
||||||
console.log(' ❌ Execution failed - No matching operator found\n');
|
console.log(`⚠️ Execution failed`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(` ❌ Error: ${error.message}\n`);
|
console.error(`❌ Error: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('🎉 Workflow Demonstration Complete!\n');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
demonstrateNestedManifold().catch(error => {
|
demonstrateNestedManifold().catch(error => {
|
||||||
console.error('❌ Fatal Error:', error);
|
console.error(`❌ Critical Error: ${error.message}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
15
lib.d.ts
vendored
15
lib.d.ts
vendored
@@ -2,25 +2,20 @@ export interface QueryResult {
|
|||||||
confidence: number;
|
confidence: number;
|
||||||
action: string;
|
action: string;
|
||||||
}
|
}
|
||||||
|
export interface IntentService {
|
||||||
export interface LLMService {
|
|
||||||
query(prompt: string): Promise<QueryResult>;
|
query(prompt: string): Promise<QueryResult>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface State {
|
export interface State {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
export class DummyIntentService implements IntentService {
|
||||||
export class DummyLlmService implements LLMService {
|
|
||||||
query(prompt: string): Promise<QueryResult>;
|
query(prompt: string): Promise<QueryResult>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WorkflowOperator {
|
export class WorkflowOperator {
|
||||||
constructor(name: string, operation: (state: State) => Promise<State>);
|
constructor(name: string, operation: (state: State) => Promise<State>);
|
||||||
name: string;
|
name: string;
|
||||||
execute(state: State): Promise<State>;
|
execute(state: State): Promise<State>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ManifoldRegion {
|
export class ManifoldRegion {
|
||||||
constructor(name: string, operators?: WorkflowOperator[]);
|
constructor(name: string, operators?: WorkflowOperator[]);
|
||||||
name: string;
|
name: string;
|
||||||
@@ -30,17 +25,15 @@ export class ManifoldRegion {
|
|||||||
connectTo(region: ManifoldRegion): void;
|
connectTo(region: ManifoldRegion): void;
|
||||||
getValidOperators(state: State): Promise<WorkflowOperator[]>;
|
getValidOperators(state: State): Promise<WorkflowOperator[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NestedManifoldRegion extends ManifoldRegion {
|
export class NestedManifoldRegion extends ManifoldRegion {
|
||||||
constructor(name: string, nestedManifold: WorkflowFunctionManifold);
|
constructor(name: string, nestedManifold: WorkflowFunctionManifold);
|
||||||
nestedManifold: WorkflowFunctionManifold;
|
nestedManifold: WorkflowFunctionManifold;
|
||||||
navigate(prompt: string): Promise<boolean>;
|
navigate(prompt: string): Promise<boolean>;
|
||||||
executeWorkflow(prompt: string): Promise<boolean>;
|
executeWorkflow(prompt: string): Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WorkflowFunctionManifold {
|
export class WorkflowFunctionManifold {
|
||||||
constructor(llmService: LLMService);
|
constructor(intentService: IntentService);
|
||||||
llmService: LLMService;
|
intentService: IntentService;
|
||||||
regions: Map<string, ManifoldRegion | NestedManifoldRegion>;
|
regions: Map<string, ManifoldRegion | NestedManifoldRegion>;
|
||||||
currentRegion: ManifoldRegion | NestedManifoldRegion | null;
|
currentRegion: ManifoldRegion | NestedManifoldRegion | null;
|
||||||
state: State;
|
state: State;
|
||||||
|
32
lib.js
32
lib.js
@@ -1,4 +1,4 @@
|
|||||||
export class DummyLlmService {
|
export class DummyIntentService {
|
||||||
async query(prompt) {
|
async query(prompt) {
|
||||||
const intents = {
|
const intents = {
|
||||||
analyze: { confidence: 0.9, action: 'analysis' },
|
analyze: { confidence: 0.9, action: 'analysis' },
|
||||||
@@ -19,7 +19,6 @@ export class WorkflowOperator {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute(state) {
|
async execute(state) {
|
||||||
return await this.operation(state);
|
return await this.operation(state);
|
||||||
}
|
}
|
||||||
@@ -31,16 +30,13 @@ export class ManifoldRegion {
|
|||||||
this.operators = operators;
|
this.operators = operators;
|
||||||
this.adjacentRegions = new Set();
|
this.adjacentRegions = new Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
addOperator(operator) {
|
addOperator(operator) {
|
||||||
this.operators.push(operator);
|
this.operators.push(operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
connectTo(region) {
|
connectTo(region) {
|
||||||
this.adjacentRegions.add(region);
|
this.adjacentRegions.add(region);
|
||||||
region.adjacentRegions.add(this);
|
region.adjacentRegions.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getValidOperators(_state) {
|
async getValidOperators(_state) {
|
||||||
return this.operators;
|
return this.operators;
|
||||||
}
|
}
|
||||||
@@ -51,18 +47,15 @@ export class NestedManifoldRegion extends ManifoldRegion {
|
|||||||
super(name);
|
super(name);
|
||||||
this.nestedManifold = nestedManifold;
|
this.nestedManifold = nestedManifold;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getValidOperators(state) {
|
async getValidOperators(state) {
|
||||||
if (!this.nestedManifold.currentRegion) {
|
if (!this.nestedManifold.currentRegion) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
return await this.nestedManifold.currentRegion.getValidOperators(state);
|
return await this.nestedManifold.currentRegion.getValidOperators(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
async navigate(prompt) {
|
async navigate(prompt) {
|
||||||
return await this.nestedManifold.navigate(prompt);
|
return await this.nestedManifold.navigate(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
async executeWorkflow(prompt) {
|
async executeWorkflow(prompt) {
|
||||||
const result = await this.nestedManifold.executeWorkflow(prompt);
|
const result = await this.nestedManifold.executeWorkflow(prompt);
|
||||||
return result;
|
return result;
|
||||||
@@ -70,74 +63,59 @@ export class NestedManifoldRegion extends ManifoldRegion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class WorkflowFunctionManifold {
|
export class WorkflowFunctionManifold {
|
||||||
constructor(llmService) {
|
constructor(intentService) {
|
||||||
this.llmService = llmService;
|
this.intentService = intentService;
|
||||||
this.regions = new Map();
|
this.regions = new Map();
|
||||||
this.currentRegion = null;
|
this.currentRegion = null;
|
||||||
this.state = {};
|
this.state = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
addRegion(region) {
|
addRegion(region) {
|
||||||
this.regions.set(region.name, region);
|
this.regions.set(region.name, region);
|
||||||
if (!this.currentRegion) {
|
if (!this.currentRegion) {
|
||||||
this.currentRegion = region;
|
this.currentRegion = region;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async navigate(prompt) {
|
async navigate(prompt) {
|
||||||
try {
|
try {
|
||||||
// If current region is nested, try to navigate within it first
|
|
||||||
if (this.currentRegion instanceof NestedManifoldRegion) {
|
if (this.currentRegion instanceof NestedManifoldRegion) {
|
||||||
const nestedNavigated = await this.currentRegion.navigate(prompt);
|
const nestedNavigated = await this.currentRegion.navigate(prompt);
|
||||||
if (nestedNavigated) {
|
if (nestedNavigated) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const intent = await this.intentService.query(prompt);
|
||||||
const intent = await this.llmService.query(prompt);
|
|
||||||
if (intent.confidence <= 0.5) {
|
if (intent.confidence <= 0.5) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to find matching region
|
|
||||||
const nextRegion = Array.from(this.currentRegion.adjacentRegions).find(region =>
|
const nextRegion = Array.from(this.currentRegion.adjacentRegions).find(region =>
|
||||||
region.name.toLowerCase().includes(intent.action)
|
region.name.toLowerCase().includes(intent.action)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (nextRegion) {
|
if (nextRegion) {
|
||||||
this.currentRegion = nextRegion;
|
this.currentRegion = nextRegion;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Navigation error:', error);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async executeWorkflow(prompt) {
|
async executeWorkflow(prompt) {
|
||||||
try {
|
try {
|
||||||
if (this.currentRegion instanceof NestedManifoldRegion) {
|
if (this.currentRegion instanceof NestedManifoldRegion) {
|
||||||
const nestedResult = await this.currentRegion.executeWorkflow(prompt);
|
const nestedResult = await this.currentRegion.executeWorkflow(prompt);
|
||||||
return nestedResult;
|
return nestedResult;
|
||||||
}
|
}
|
||||||
|
const intent = await this.intentService.query(prompt);
|
||||||
const intent = await this.llmService.query(prompt);
|
|
||||||
const operators = await this.currentRegion.getValidOperators(this.state);
|
const operators = await this.currentRegion.getValidOperators(this.state);
|
||||||
|
|
||||||
const matchedOperator = operators.find(op =>
|
const matchedOperator = operators.find(op =>
|
||||||
op.name.toLowerCase().includes(intent.action)
|
op.name.toLowerCase().includes(intent.action)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (matchedOperator && intent.confidence > 0.5) {
|
if (matchedOperator && intent.confidence > 0.5) {
|
||||||
this.state = await matchedOperator.execute(this.state);
|
this.state = await matchedOperator.execute(this.state);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Execution error:', error);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user