MCPcopy Create free account
hub / github.com/chieapp/chie / AssistantManager

Class AssistantManager

src/controller/assistant-manager.ts:27–143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25type AssistantManagerData = Record<string, AssistantManagerDataItem>;
26
27export class AssistantManager extends ConfigStoreItem {
28 onNewAssistant: Signal<(assistant: Assistant, index: number) => void> = new Signal;
29 onRemoveAssistant: Signal<(assistant: Assistant) => void> = new Signal;
30 onReorderAssistant: Signal<(assistant: Assistant, fromIndex: number, toIndex:number) => void> = new Signal;
31
32 #assistants: Assistant[] = [];
33
34 deserialize(data: AssistantManagerData) {
35 if (!data) // accepts empty data
36 data = {};
37 if (typeof data != 'object')
38 throw new Error(`Unknown data for "services": ${JSON.stringify(data)}.`);
39 this.#assistants = [];
40 for (const id in data) {
41 const item = data[id];
42 if (typeof item.serviceName != 'string' ||
43 typeof item.service != 'object' ||
44 typeof item.view != 'string')
45 throw new Error(`Unknown data for Assistant: ${JSON.stringify(item)}.`);
46 // Get the service type first.
47 const serviceName = item.serviceName;
48 const record = serviceManager.getServiceByName(serviceName);
49 if (!record)
50 throw new Error(`Unknown service "${serviceName}".`);
51 // Check view type.
52 const viewClass = serviceManager.getRegisteredViews().find(v => v.name == item.view);
53 if (!viewClass)
54 throw new Error(`Unknown View "${item.view}".`);
55 // Find out a deserialize method in the prototype chain.
56 let baseClass = record.serviceClass;
57 while (!baseClass.deserialize && baseClass != WebService)
58 baseClass = baseClass.prototype;
59 if (!baseClass.deserialize)
60 throw new Error(`Can not find a deserialize method for service "${serviceName}".`);
61 // Deserialize using the service type's method.
62 const options = baseClass.deserialize(item.service);
63 const service = new record.serviceClass(options);
64 const assistant = new Assistant(id, service, viewClass);
65 if (item.shortcut)
66 assistant.setShortcut(item.shortcut);
67 if (item.hasTray) {
68 const icon = item.trayIcon ? new Icon({chieURL: item.trayIcon}) : service.icon;
69 assistant.setTrayIcon(icon);
70 }
71 this.#assistants.push(assistant);
72 }
73 }
74
75 serialize() {
76 const data: AssistantManagerData = {};
77 for (const assistant of this.#assistants) {
78 const item: AssistantManagerDataItem = {
79 serviceName: assistant.service.constructor.name,
80 service: assistant.service.serialize(),
81 view: assistant.viewClass.name,
82 };
83 if (assistant.shortcut)
84 item.shortcut = assistant.shortcut;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected