Spaces:
Running
Running
File size: 2,460 Bytes
b39afbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
/**
* Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
* All rights reserved.
*/
import { WorkflowClientControlManager } from './WorkflowClientControlManager.js';
import { OAIBaseComponent } from './openapi/OAIComponent31.js';
class WorkflowComponentRegistry {
components: Map<string, OAIBaseComponent>;
clientComponents: Map<string, any>;
ctors: Map<string, any>;
loaded: boolean = false; // May not be fully functional yet, use at own risk!!
constructor() {
this.components = new Map();
this.clientComponents = new Map();
this.ctors = new Map();
}
registerCtor(type: string, Ctor: any): void {
this.ctors.set(type, Ctor);
}
create(definitions: any, namespace?: string) {
let components: OAIBaseComponent[] = [];
components = definitions
.map((definition: any) => {
if (definition instanceof OAIBaseComponent) {
return definition;
}
definition.type ??= 'OAIBaseComponent';
if (this.ctors.has(definition.type)) {
const Ctor = this.ctors.get(definition.type);
return Ctor.fromJSON(definition);
}
return null;
})
.filter((c: OAIBaseComponent) => c !== null);
return components;
}
add(definitions: any) {
this.create(definitions).forEach((component: OAIBaseComponent) => {
this.components.set(component.name, component);
});
return this;
}
registerClientComponent(key: string, clientComponent: any) {
this.clientComponents.set(key, clientComponent);
}
hasClientComponent(key: string) {
return this.clientComponents.has(key);
}
getClientComponent(key: string) {
return this.clientComponents.get(key);
}
get(name: string) {
return this.components.get(name);
}
has(name: string) {
return this.components.has(name);
}
getComponents(all?: boolean): OAIBaseComponent[] {
let ret = Array.from(this.components.values());
if (!all) {
ret = ret.filter((c: OAIBaseComponent) => c.tags.includes('default'));
}
return ret;
}
getControlRegistry(): WorkflowClientControlManager {
return WorkflowClientControlManager.getInstance();
}
// Singleton pattern
static instance: WorkflowComponentRegistry;
static getSingleton(): WorkflowComponentRegistry {
WorkflowComponentRegistry.instance ??= new WorkflowComponentRegistry();
return WorkflowComponentRegistry.instance;
}
}
export { WorkflowComponentRegistry };
|