Refactor project structure and update build scripts
Moved main files to the "src" directory and updated paths accordingly. Adjusted build, start, and dev scripts to use Bun. Added "dist" directory to .gitignore to prevent it from being tracked.
This commit is contained in:
76
src/cli.ts
Normal file
76
src/cli.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import {
|
||||
DummyIntentMap,
|
||||
ManifoldRegion,
|
||||
WorkflowFunctionManifold,
|
||||
WorkflowOperator,
|
||||
NestedManifoldRegion,
|
||||
} from './index';
|
||||
|
||||
async function demonstrateNestedManifold() {
|
||||
const nestedIntentService = new DummyIntentMap();
|
||||
const nestedManifold = new WorkflowFunctionManifold(nestedIntentService);
|
||||
|
||||
const validateOp = new WorkflowOperator('validation', async (state: any) => {
|
||||
return { ...state, validated: true };
|
||||
});
|
||||
const cleanOp = new WorkflowOperator('cleaning', async (state: any) => {
|
||||
return { ...state, cleaned: true };
|
||||
});
|
||||
|
||||
const validateRegion = new ManifoldRegion('validation', [validateOp]);
|
||||
const cleanRegion = new ManifoldRegion('cleaning', [cleanOp]);
|
||||
|
||||
validateRegion.connectTo(cleanRegion);
|
||||
nestedManifold.addRegion(validateRegion);
|
||||
nestedManifold.addRegion(cleanRegion);
|
||||
|
||||
const mainIntentService = new DummyIntentMap();
|
||||
const mainManifold = new WorkflowFunctionManifold(mainIntentService);
|
||||
|
||||
const analysisOp = new WorkflowOperator('analysis', async (state: any) => {
|
||||
return { ...state, analyzed: true };
|
||||
});
|
||||
const transformOp = new WorkflowOperator('transformation', async (state: any) => {
|
||||
return { ...state, transformed: true };
|
||||
});
|
||||
|
||||
const nestedPreprocessRegion = new NestedManifoldRegion('preprocessing', nestedManifold);
|
||||
const analysisRegion = new ManifoldRegion('analysis', [analysisOp]);
|
||||
const transformRegion = new ManifoldRegion('transformation', [transformOp]);
|
||||
|
||||
nestedPreprocessRegion.connectTo(analysisRegion);
|
||||
analysisRegion.connectTo(transformRegion);
|
||||
|
||||
mainManifold.addRegion(nestedPreprocessRegion);
|
||||
mainManifold.addRegion(analysisRegion);
|
||||
mainManifold.addRegion(transformRegion);
|
||||
|
||||
const prompts = [
|
||||
{ text: 'validate the input', description: 'Nested: Data Validation' },
|
||||
{ text: 'clean the data', description: 'Nested: Data Cleaning' },
|
||||
{ text: 'analyze the results', description: 'Main: Data Analysis' },
|
||||
{ text: 'transform the output', description: 'Main: Data Transformation' },
|
||||
];
|
||||
|
||||
for (const { text, description } of prompts) {
|
||||
try {
|
||||
const navigated = await mainManifold.navigate(text);
|
||||
if (navigated) {
|
||||
console.log(`📍 Step: ${description}`);
|
||||
}
|
||||
const executed = await mainManifold.executeWorkflow(text);
|
||||
if (executed) {
|
||||
console.log(`✅ Execution complete`);
|
||||
} else {
|
||||
console.log(`⚠️ Execution failed`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ Error: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
demonstrateNestedManifold().catch(error => {
|
||||
console.error(`❌ Critical Error: ${error.message}`);
|
||||
process.exit(1);
|
||||
});
|
146
src/index.ts
Normal file
146
src/index.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
export class DummyIntentMap {
|
||||
async query(prompt: string): Promise<{ confidence: number; action: string }> {
|
||||
const intents: Record<string, { confidence: number; action: string }> = {
|
||||
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 {
|
||||
name: string;
|
||||
operation: (state: any) => Promise<any>;
|
||||
|
||||
constructor(name: string, operation: (state: any) => Promise<any>) {
|
||||
this.name = name;
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
async execute(state: any): Promise<any> {
|
||||
return await this.operation(state);
|
||||
}
|
||||
}
|
||||
|
||||
export class ManifoldRegion {
|
||||
name: string;
|
||||
operators: WorkflowOperator[];
|
||||
adjacentRegions: Set<ManifoldRegion>;
|
||||
|
||||
constructor(name: string, operators: WorkflowOperator[] = []) {
|
||||
this.name = name;
|
||||
this.operators = operators;
|
||||
this.adjacentRegions = new Set<ManifoldRegion>();
|
||||
}
|
||||
|
||||
addOperator(operator: WorkflowOperator): void {
|
||||
this.operators.push(operator);
|
||||
}
|
||||
|
||||
connectTo(region: ManifoldRegion): void {
|
||||
this.adjacentRegions.add(region);
|
||||
region.adjacentRegions.add(this);
|
||||
}
|
||||
|
||||
async getValidOperators(_state: any): Promise<WorkflowOperator[]> {
|
||||
return this.operators;
|
||||
}
|
||||
}
|
||||
|
||||
export class NestedManifoldRegion extends ManifoldRegion {
|
||||
nestedManifold: WorkflowFunctionManifold;
|
||||
|
||||
constructor(name: string, nestedManifold: WorkflowFunctionManifold) {
|
||||
super(name);
|
||||
this.nestedManifold = nestedManifold;
|
||||
}
|
||||
|
||||
async getValidOperators(state: any): Promise<WorkflowOperator[]> {
|
||||
if (!this.nestedManifold.currentRegion) {
|
||||
return [];
|
||||
}
|
||||
return await this.nestedManifold.currentRegion.getValidOperators(state);
|
||||
}
|
||||
|
||||
async navigate(prompt: string): Promise<boolean> {
|
||||
return await this.nestedManifold.navigate(prompt);
|
||||
}
|
||||
|
||||
async executeWorkflow(prompt: string): Promise<boolean> {
|
||||
const result = await this.nestedManifold.executeWorkflow(prompt);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export class WorkflowFunctionManifold {
|
||||
intentMap: DummyIntentMap;
|
||||
regions: Map<string, ManifoldRegion>;
|
||||
currentRegion: ManifoldRegion | null;
|
||||
state: any;
|
||||
|
||||
constructor(intentMap: DummyIntentMap) {
|
||||
this.intentMap = intentMap;
|
||||
this.regions = new Map<string, ManifoldRegion>();
|
||||
this.currentRegion = null;
|
||||
this.state = {};
|
||||
}
|
||||
|
||||
addRegion(region: ManifoldRegion): void {
|
||||
this.regions.set(region.name, region);
|
||||
if (!this.currentRegion) {
|
||||
this.currentRegion = region;
|
||||
}
|
||||
}
|
||||
|
||||
async navigate(prompt: string): Promise<boolean> {
|
||||
try {
|
||||
if (this.currentRegion instanceof NestedManifoldRegion) {
|
||||
const nestedNavigated = await this.currentRegion.navigate(prompt);
|
||||
if (nestedNavigated) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
const intent = await this.intentMap.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: string): Promise<boolean> {
|
||||
try {
|
||||
if (this.currentRegion instanceof NestedManifoldRegion) {
|
||||
const nestedResult = await this.currentRegion.executeWorkflow(prompt);
|
||||
return nestedResult;
|
||||
}
|
||||
const intent = await this.intentMap.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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user