switch to typescript
This commit is contained in:
146
index.ts
Normal file
146
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