added documentation inline with code

This commit is contained in:
2024-11-09 11:35:34 -05:00
parent 5003cafb87
commit 1541b63d85

67
lib.js
View File

@@ -1,7 +1,14 @@
// Simulated LLM Service // Simulated LLM Service
/**
* Simulates a Large Language Model (LLM) service for intent detection.
*/
export class DummyLlmService { export class DummyLlmService {
/**
* Queries the simulated LLM with a prompt.
* @param {string} prompt - The input prompt to query the LLM.
* @returns {Promise<{confidence: number, action: string}>} - The detected intent with confidence and action.
*/
async query(prompt) { async query(prompt) {
// Simulate LLM response with basic intent matching
const intents = { const intents = {
'analyze': { confidence: 0.9, action: 'analysis' }, 'analyze': { confidence: 0.9, action: 'analysis' },
'process': { confidence: 0.8, action: 'processing' }, 'process': { confidence: 0.8, action: 'processing' },
@@ -16,41 +23,82 @@ export class DummyLlmService {
} }
// Interface for workflow operators // Interface for workflow operators
/**
* Represents a workflow operator responsible for performing a specific operation.
*/
export class WorkflowOperator { export class WorkflowOperator {
/**
* Creates an instance of WorkflowOperator.
* @param {string} name - The name of the operator.
* @param {function(object): Promise<object>} operation - The operation function executed by the operator.
*/
constructor(name, operation) { constructor(name, operation) {
this.name = name; this.name = name;
this.operation = operation; this.operation = operation;
} }
/**
* Executes the operator's operation with the given state.
* @param {object} state - The current state to be processed.
* @returns {Promise<object>} - The updated state after the operation.
*/
async execute(state) { async execute(state) {
return await this.operation(state); return await this.operation(state);
} }
} }
// Interface for manifold regions // Interface for manifold regions
/**
* Represents a region within the manifold, containing workflow operators.
*/
export class ManifoldRegion { export class ManifoldRegion {
/**
* Creates an instance of ManifoldRegion.
* @param {string} name - The name of the region.
* @param {WorkflowOperator[]} [operators=[]] - The operators available in this region.
*/
constructor(name, operators = []) { constructor(name, operators = []) {
this.name = name; this.name = name;
this.operators = operators; this.operators = operators;
this.adjacentRegions = new Set(); this.adjacentRegions = new Set();
} }
/**
* Adds an operator to the region.
* @param {WorkflowOperator} operator - The operator to be added.
*/
addOperator(operator) { addOperator(operator) {
this.operators.push(operator); this.operators.push(operator);
} }
/**
* Establishes a connection to another region.
* @param {ManifoldRegion} region - The region to connect to.
*/
connectTo(region) { connectTo(region) {
this.adjacentRegions.add(region); this.adjacentRegions.add(region);
region.adjacentRegions.add(this); region.adjacentRegions.add(this);
} }
/**
* Retrieves valid operators for the given state.
* @param {object} state - The current state.
* @returns {Promise<WorkflowOperator[]>} - The list of valid operators.
*/
async getValidOperators(state) { async getValidOperators(state) {
return this.operators; return this.operators;
} }
} }
// Main manifold implementation // Main manifold implementation
/**
* Represents the workflow function manifold managing regions and state transitions.
*/
export class WorkflowFunctionManifold { export class WorkflowFunctionManifold {
/**
* Creates an instance of WorkflowFunctionManifold.
* @param {DummyLlmService} llmService - The LLM service used for intent detection.
*/
constructor(llmService) { constructor(llmService) {
this.llmService = llmService; this.llmService = llmService;
this.regions = new Map(); this.regions = new Map();
@@ -58,6 +106,10 @@ export class WorkflowFunctionManifold {
this.state = {}; this.state = {};
} }
/**
* Adds a region to the manifold.
* @param {ManifoldRegion} region - The region to be added.
*/
addRegion(region) { addRegion(region) {
this.regions.set(region.name, region); this.regions.set(region.name, region);
if (!this.currentRegion) { if (!this.currentRegion) {
@@ -65,11 +117,15 @@ export class WorkflowFunctionManifold {
} }
} }
/**
* Navigates to the next region based on the provided prompt.
* @param {string} prompt - The input prompt for intent matching.
* @returns {Promise<boolean>} - Whether navigation was successful.
*/
async navigate(prompt) { async navigate(prompt) {
try { try {
const intent = await this.llmService.query(prompt); const intent = await this.llmService.query(prompt);
// Find the best matching adjacent region
const nextRegion = Array.from(this.currentRegion.adjacentRegions) const nextRegion = Array.from(this.currentRegion.adjacentRegions)
.find(region => region.name.toLowerCase().includes(intent.action)); .find(region => region.name.toLowerCase().includes(intent.action));
@@ -86,6 +142,11 @@ export class WorkflowFunctionManifold {
} }
} }
/**
* Executes the workflow based on the current region and provided prompt.
* @param {string} prompt - The input prompt for intent matching.
* @returns {Promise<boolean>} - Whether the workflow execution was successful.
*/
async executeWorkflow(prompt) { async executeWorkflow(prompt) {
try { try {
const operators = await this.currentRegion.getValidOperators(this.state); const operators = await this.currentRegion.getValidOperators(this.state);
@@ -105,4 +166,4 @@ export class WorkflowFunctionManifold {
return false; return false;
} }
} }
} }