Introduced the `NestedManifoldRegion` class to enable embedding of `WorkflowFunctionManifold` inside regions, allowing hierarchical workflows. Updated `bin.js` to demonstrate the new functionality and adjusted `lib.js` for nested region navigation and execution logic. Updated `package.json` to specify "type": "module" for ES module support.
144 lines
3.6 KiB
JavaScript
144 lines
3.6 KiB
JavaScript
export class DummyLlmService {
|
|
async query(prompt) {
|
|
const intents = {
|
|
analyze: { confidence: 0.9, action: 'analysis' },
|
|
process: { confidence: 0.8, action: 'processing' },
|
|
transform: { confidence: 0.7, action: 'transformation' },
|
|
validate: { confidence: 0.85, action: 'validation' },
|
|
clean: { confidence: 0.85, action: 'cleaning' },
|
|
};
|
|
const matchedIntent = Object.entries(intents).find(([key]) =>
|
|
prompt.toLowerCase().includes(key)
|
|
);
|
|
return matchedIntent ? matchedIntent[1] : { confidence: 0.1, action: 'unknown' };
|
|
}
|
|
}
|
|
|
|
export class WorkflowOperator {
|
|
constructor(name, operation) {
|
|
this.name = name;
|
|
this.operation = operation;
|
|
}
|
|
|
|
async execute(state) {
|
|
return await this.operation(state);
|
|
}
|
|
}
|
|
|
|
export class ManifoldRegion {
|
|
constructor(name, operators = []) {
|
|
this.name = name;
|
|
this.operators = operators;
|
|
this.adjacentRegions = new Set();
|
|
}
|
|
|
|
addOperator(operator) {
|
|
this.operators.push(operator);
|
|
}
|
|
|
|
connectTo(region) {
|
|
this.adjacentRegions.add(region);
|
|
region.adjacentRegions.add(this);
|
|
}
|
|
|
|
async getValidOperators(_state) {
|
|
return this.operators;
|
|
}
|
|
}
|
|
|
|
export class NestedManifoldRegion extends ManifoldRegion {
|
|
constructor(name, nestedManifold) {
|
|
super(name);
|
|
this.nestedManifold = nestedManifold;
|
|
}
|
|
|
|
async getValidOperators(state) {
|
|
if (!this.nestedManifold.currentRegion) {
|
|
return [];
|
|
}
|
|
return await this.nestedManifold.currentRegion.getValidOperators(state);
|
|
}
|
|
|
|
async navigate(prompt) {
|
|
return await this.nestedManifold.navigate(prompt);
|
|
}
|
|
|
|
async executeWorkflow(prompt) {
|
|
const result = await this.nestedManifold.executeWorkflow(prompt);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
export class WorkflowFunctionManifold {
|
|
constructor(llmService) {
|
|
this.llmService = llmService;
|
|
this.regions = new Map();
|
|
this.currentRegion = null;
|
|
this.state = {};
|
|
}
|
|
|
|
addRegion(region) {
|
|
this.regions.set(region.name, region);
|
|
if (!this.currentRegion) {
|
|
this.currentRegion = region;
|
|
}
|
|
}
|
|
|
|
async navigate(prompt) {
|
|
try {
|
|
// If current region is nested, try to navigate within it first
|
|
if (this.currentRegion instanceof NestedManifoldRegion) {
|
|
const nestedNavigated = await this.currentRegion.navigate(prompt);
|
|
if (nestedNavigated) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
const intent = await this.llmService.query(prompt);
|
|
if (intent.confidence <= 0.5) {
|
|
return false;
|
|
}
|
|
|
|
// Try to find matching region
|
|
const nextRegion = Array.from(this.currentRegion.adjacentRegions).find(region =>
|
|
region.name.toLowerCase().includes(intent.action)
|
|
);
|
|
|
|
if (nextRegion) {
|
|
this.currentRegion = nextRegion;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
} catch (error) {
|
|
console.error('Navigation error:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async executeWorkflow(prompt) {
|
|
try {
|
|
if (this.currentRegion instanceof NestedManifoldRegion) {
|
|
const nestedResult = await this.currentRegion.executeWorkflow(prompt);
|
|
return nestedResult;
|
|
}
|
|
|
|
const intent = await this.llmService.query(prompt);
|
|
const operators = await this.currentRegion.getValidOperators(this.state);
|
|
|
|
const matchedOperator = operators.find(op =>
|
|
op.name.toLowerCase().includes(intent.action)
|
|
);
|
|
|
|
if (matchedOperator && intent.confidence > 0.5) {
|
|
this.state = await matchedOperator.execute(this.state);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
} catch (error) {
|
|
console.error('Execution error:', error);
|
|
return false;
|
|
}
|
|
}
|
|
} |