Files
manifold-workflow-engine/lib.js
Geoff Seemueller 2712ffecf4 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.
2024-11-14 15:26:51 -05:00

122 lines
3.4 KiB
JavaScript

export class DummyIntentService {
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(intentService) {
this.intentService = intentService;
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 (this.currentRegion instanceof NestedManifoldRegion) {
const nestedNavigated = await this.currentRegion.navigate(prompt);
if (nestedNavigated) {
return true;
}
}
const intent = await this.intentService.query(prompt);
if (intent.confidence <= 0.5) {
return false;
}
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) {
return false;
}
}
async executeWorkflow(prompt) {
try {
if (this.currentRegion instanceof NestedManifoldRegion) {
const nestedResult = await this.currentRegion.executeWorkflow(prompt);
return nestedResult;
}
const intent = await this.intentService.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) {
return false;
}
}
}