@pulse-editor/shared-utils 0.1.1-alpha.7 → 0.1.1-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/imc/inter-module-communication.d.ts +12 -5
- package/dist/imc/message-receiver.d.ts +2 -4
- package/dist/imc/message-sender.d.ts +6 -4
- package/dist/imc/poly-imc.d.ts +33 -0
- package/dist/main.d.ts +5 -4
- package/dist/main.js +1 -1
- package/dist/types/types.d.ts +62 -32
- package/package.json +1 -1
|
@@ -2,17 +2,24 @@ import { IMCMessageTypeEnum, ReceiverHandlerMap } from "../types/types";
|
|
|
2
2
|
export declare class InterModuleCommunication {
|
|
3
3
|
private thisWindow;
|
|
4
4
|
private otherWindow;
|
|
5
|
-
private thisPendingTasks;
|
|
6
|
-
private otherPendingMessages;
|
|
7
5
|
private receiver;
|
|
8
6
|
private sender;
|
|
9
|
-
private
|
|
7
|
+
private thisWindowId;
|
|
8
|
+
private otherWindowId;
|
|
10
9
|
private receiverHandlerMap;
|
|
11
10
|
private listener;
|
|
12
|
-
constructor(
|
|
13
|
-
|
|
11
|
+
constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Initialize a receiver to receive message.
|
|
14
|
+
* @param window The current window.
|
|
15
|
+
* @param otherWindowId The ID of the other window. This is optional and can be undefined.
|
|
16
|
+
* When defined, the receiver will only receive messages from window with the ID.
|
|
17
|
+
*/
|
|
18
|
+
initThisWindow(window: Window, otherWindowId?: string): void;
|
|
14
19
|
initOtherWindow(window: Window): void;
|
|
15
20
|
close(): void;
|
|
16
21
|
sendMessage(type: IMCMessageTypeEnum, payload?: any, abortSignal?: AbortSignal): Promise<any>;
|
|
17
22
|
updateReceiverHandlerMap(receiverHandlerMap: ReceiverHandlerMap): void;
|
|
23
|
+
getThisWindowId(): string;
|
|
24
|
+
getOtherWindowId(): string;
|
|
18
25
|
}
|
|
@@ -2,10 +2,8 @@ import { IMCMessage, ReceiverHandlerMap } from "../types/types";
|
|
|
2
2
|
export declare class MessageReceiver {
|
|
3
3
|
private handlerMap;
|
|
4
4
|
private pendingTasks;
|
|
5
|
-
private
|
|
6
|
-
constructor(listenerMap: ReceiverHandlerMap,
|
|
7
|
-
controller: AbortController;
|
|
8
|
-
}>, moduleName: string);
|
|
5
|
+
private moduleId;
|
|
6
|
+
constructor(listenerMap: ReceiverHandlerMap, moduleId: string);
|
|
9
7
|
receiveMessage(senderWindow: Window, message: IMCMessage): void;
|
|
10
8
|
private acknowledgeSender;
|
|
11
9
|
}
|
|
@@ -3,10 +3,12 @@ export declare class MessageSender {
|
|
|
3
3
|
private targetWindow;
|
|
4
4
|
private timeout;
|
|
5
5
|
private pendingMessages;
|
|
6
|
-
private
|
|
7
|
-
constructor(targetWindow: Window, timeout: number,
|
|
6
|
+
private moduleId;
|
|
7
|
+
constructor(targetWindow: Window, timeout: number, moduleId: string);
|
|
8
|
+
sendMessage(handlingType: IMCMessageTypeEnum, payload?: any, abortSignal?: AbortSignal): Promise<any>;
|
|
9
|
+
getPendingMessage(id: string): {
|
|
8
10
|
resolve: (result: any) => void;
|
|
9
11
|
reject: () => void;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
+
} | undefined;
|
|
13
|
+
removePendingMessage(id: string): void;
|
|
12
14
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { IMCMessage, IMCMessageTypeEnum, ReceiverHandlerMap } from "../types/types";
|
|
2
|
+
/**
|
|
3
|
+
* Poly IMC establishes two-way inter-module communication channels,
|
|
4
|
+
* where each channel consists of a sender and a receiver to send and
|
|
5
|
+
* receive messages between host window and the other module's window.
|
|
6
|
+
*
|
|
7
|
+
* This assumes the other window has IMC or Poly-IMC setup.
|
|
8
|
+
*/
|
|
9
|
+
export declare class PolyIMC {
|
|
10
|
+
private channels;
|
|
11
|
+
private baseReceiverHandlerMap;
|
|
12
|
+
private channelReceiverHandlerMapMap;
|
|
13
|
+
private connectionListener;
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param baseReceiverHandlerMap A base receiver handler map for universal handlers.
|
|
17
|
+
* E.g. Pulse Editor API handler
|
|
18
|
+
*/
|
|
19
|
+
constructor(baseReceiverHandlerMap: ReceiverHandlerMap, isAutoConnect?: boolean);
|
|
20
|
+
sendMessage(targetWindowId: string, handlingType: IMCMessageTypeEnum, payload?: any, abortSignal?: AbortSignal): Promise<any>;
|
|
21
|
+
updateBaseReceiverHandlerMap(handlerMap: ReceiverHandlerMap): void;
|
|
22
|
+
updateChannelReceiverHandlerMap(targetWindowId: string, handlerMap: ReceiverHandlerMap): void;
|
|
23
|
+
createChannel(targetWindow: Window, targetWindowId: string, receiverHandlerMap?: ReceiverHandlerMap): Promise<void>;
|
|
24
|
+
removeChannel(targetWindowId: string): Promise<void>;
|
|
25
|
+
hasChannel(targetWindowId: string): boolean;
|
|
26
|
+
private getCombinedHandlerMap;
|
|
27
|
+
private getConnectionListenerHandlerMap;
|
|
28
|
+
}
|
|
29
|
+
export declare class ConnectionListener {
|
|
30
|
+
private listener;
|
|
31
|
+
constructor(polyIMC: PolyIMC, newConnectionReceiverHandlerMap: ReceiverHandlerMap, onConnection?: (senderWindow: Window, message: IMCMessage) => void);
|
|
32
|
+
close(): void;
|
|
33
|
+
}
|
package/dist/main.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { InterModuleCommunication } from "./imc/inter-module-communication";
|
|
2
|
-
import { MessageSender } from "./imc/message-sender";
|
|
3
2
|
import { MessageReceiver } from "./imc/message-receiver";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export
|
|
3
|
+
import { MessageSender } from "./imc/message-sender";
|
|
4
|
+
import { PolyIMC, ConnectionListener } from "./imc/poly-imc";
|
|
5
|
+
export * from "./types/constants";
|
|
6
|
+
export * from "./types/types";
|
|
7
|
+
export { InterModuleCommunication, MessageReceiver, MessageSender, PolyIMC, ConnectionListener, };
|
package/dist/main.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t,i
|
|
1
|
+
var e,n,t,i;function s(e){return Array.isArray(e)&&1===e.length}function r(e){return"object"==typeof e&&!Array.isArray(e)}!function(e){e.WriteViewFile="write-view-file",e.RequestViewFile="request-view-file",e.Fetch="fetch",e.Notification="notification",e.ThemeChange="theme-change",e.RunAgentMethod="run-agent-method",e.UseVAD="use-vad",e.UseSTT="use-stt",e.UseLLM="use-llm",e.UseTTS="use-tts",e.UseDiffusion="use-diffusion",e.UseOCR="use-ocr",e.UseSpeech2Speech="use-speech2speech",e.RunExtCommand="run-ext-command",e.RequestTerminal="request-terminal",e.ExtReady="ext-ready",e.ExtClose="ext-close",e.Loaded="loaded",e.Acknowledge="acknowledge",e.Abort="abort",e.Error="error"}(e||(e={})),function(e){e.Success="success",e.Error="error",e.Info="info",e.Warning="warning"}(n||(n={})),function(e){e.Generic="generic",e.FileView="file-view",e.ConsoleView="console-view"}(t||(t={})),function(e){e.public="public",e.private="private"}(i||(i={}));const o=3e5;class a{constructor(e,n){this.handlerMap=e,this.pendingTasks=new Map,this.moduleId=n}receiveMessage(n,t){if(this.moduleId===t.from)return;if("development"===process.env.NODE_ENV&&console.log(`Module ${this.moduleId} received message from module ${t.from}:\n ${JSON.stringify(t)}`),t.type===e.Abort){const e=t.id,n=this.pendingTasks.get(e);return void(n&&(console.log("Aborting task",e),n.controller.abort(),this.pendingTasks.delete(e)))}const i=this.handlerMap.get(t.type);if(i){const s=new AbortController,r=s.signal;this.pendingTasks.set(t.id,{controller:s});i(n,t,r).then((i=>{r.aborted||t.type!==e.Acknowledge&&this.acknowledgeSender(n,t.id,i)})).catch((i=>{const s={id:t.id,type:e.Error,payload:i.message,from:this.moduleId};n.postMessage(s,"*")})).finally((()=>{this.pendingTasks.delete(t.id)}))}}acknowledgeSender(n,t,i){const s={id:t,type:e.Acknowledge,payload:i,from:this.moduleId};n.postMessage(s,"*")}}class d{constructor(e,n,t){this.targetWindow=e,this.timeout=n,this.pendingMessages=new Map,this.moduleId=t}async sendMessage(n,t,i){const s=(new Date).getTime().toString(),r={id:s,type:n,payload:t,from:this.moduleId};return new Promise(((n,t)=>{if(i?.aborted)return t(new Error("Request aborted"));const o=()=>{this.pendingMessages.delete(s),this.targetWindow.postMessage({id:s,type:e.Abort,payload:JSON.stringify({status:"Task aborted",data:null})},"*"),t(new Error("Request aborted"))};i?.addEventListener("abort",o),this.pendingMessages.set(s,{resolve:n,reject:t}),this.targetWindow.postMessage(r,"*");const a=setTimeout((()=>{this.pendingMessages.delete(s),i?.removeEventListener("abort",o),t(new Error("Communication with Pulse Editor timeout."))}),this.timeout),d=this.pendingMessages.get(s);d&&(d.resolve=e=>{clearTimeout(a),i?.removeEventListener("abort",o),n(e)},d.reject=()=>{clearTimeout(a),i?.removeEventListener("abort",o),t()})}))}getPendingMessage(e){return this.pendingMessages.get(e)}removePendingMessage(e){this.pendingMessages.delete(e)}}class h{constructor(){}initThisWindow(e,n){this.thisWindow=e;const t=e.viewId;if(!t)throw new Error("Current window's ID is not defined.");this.thisWindowId=t,this.receiverHandlerMap=new Map;const i=new a(this.receiverHandlerMap,this.thisWindowId);this.receiver=i,this.listener=e=>{if(!i)throw new Error("Receiver not initialized at module "+this.thisWindowId);const t=e.data;if(n&&t.from!==n)return;const s=e.source;i.receiveMessage(s,t)},e.addEventListener("message",this.listener),console.log("Adding IMC listener in "+this.thisWindowId)}initOtherWindow(n){if(!this.thisWindow||!this.thisWindowId)throw new Error("You must initialize the current window first.");this.otherWindow=n;const t=n.viewId;if(!t)throw new Error("Other window's ID is not defined.");this.otherWindowId=t;const i=new d(n,o,this.thisWindowId);if(this.sender=i,!this.receiverHandlerMap)throw new Error("You must initialize the current window first.");this.receiverHandlerMap.set(e.Acknowledge,(async(e,n)=>{const t=i.getPendingMessage(n.id);t&&(t.resolve(n.payload),i.removePendingMessage(n.id))}))}close(){this.listener&&window.removeEventListener("message",this.listener)}async sendMessage(e,n,t){const i=this.sender;if(!i)throw new Error("Sender not initialized");return await i.sendMessage(e,n,t)}updateReceiverHandlerMap(n){if(!this.receiver)throw new Error("Receiver not initialized");this.receiverHandlerMap?.clear(),this.receiverHandlerMap?.set(e.Acknowledge,(async(e,n)=>{const t=this.sender?.getPendingMessage(n.id);t&&(t.resolve(n.payload),this.sender?.removePendingMessage(n.id))})),n.forEach(((e,n)=>{this.receiverHandlerMap?.set(n,e)}))}getThisWindowId(){if(!this.thisWindowId)throw new Error("This window ID is not defined.");return this.thisWindowId}getOtherWindowId(){if(!this.otherWindowId)throw new Error("Other window ID is not defined.");return this.otherWindowId}}class c{constructor(e,n=!1){this.channels=new Map,this.baseReceiverHandlerMap=e,this.channelReceiverHandlerMapMap=new Map,n&&(this.connectionListener=new h,this.connectionListener.initThisWindow(window),this.connectionListener.updateReceiverHandlerMap(this.getConnectionListenerHandlerMap()))}async sendMessage(e,n,t,i){const s=this.channels.get(e);if(!s)throw new Error("Channel not found for window ID "+e);return await s.sendMessage(n,t,i)}updateBaseReceiverHandlerMap(e){this.baseReceiverHandlerMap=e,this.channels.forEach(((e,n)=>{const t=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,this.channelReceiverHandlerMapMap.get(n));e.updateReceiverHandlerMap(t)}))}updateChannelReceiverHandlerMap(e,n){const t=this.channels.get(e);if(!t)throw new Error("Channel not found for window ID "+e);const i=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,n);t.updateReceiverHandlerMap(i)}async createChannel(e,n,t){const i=new h;i.initThisWindow(window,n),i.initOtherWindow(e),t?(i.updateReceiverHandlerMap(this.getCombinedHandlerMap(this.baseReceiverHandlerMap,t)),this.channelReceiverHandlerMapMap.set(n,t)):i.updateReceiverHandlerMap(this.baseReceiverHandlerMap),this.channels.set(n,i)}async removeChannel(e){const n=this.channels.get(e);if(!n)throw new Error("Channel not found for window ID "+e);n.close(),this.channels.delete(e),this.channelReceiverHandlerMapMap.delete(e)}hasChannel(e){return this.channels.has(e)}getCombinedHandlerMap(e,n){if(e&&n){return new Map([...e,...n])}return e||(n||new Map)}getConnectionListenerHandlerMap(){return new Map([[e.ExtReady,async(e,n)=>{const t=n.from;this.channels.has(t)?console.log("Channel already exists for window ID "+t+". Re-using the existing channel."):await this.createChannel(e,t,this.getCombinedHandlerMap(this.baseReceiverHandlerMap,this.channelReceiverHandlerMapMap.get(t)))}]])}}class l{constructor(n,t,i){const s=new h;this.listener=s,s.initThisWindow(window),s.updateReceiverHandlerMap(new Map([[e.ExtReady,async(e,s,r)=>{const o=s.from;n.hasChannel(o)?console.log("Channel already exists for window ID "+o+". Re-using the existing channel."):(await n.createChannel(e,o,t),i&&i(e,s))}]]))}close(){this.listener?.close()}}export{i as AccessEnum,l as ConnectionListener,t as ExtensionTypeEnum,e as IMCMessageTypeEnum,h as InterModuleCommunication,a as MessageReceiver,d as MessageSender,n as NotificationTypeEnum,c as PolyIMC,s as isArrayType,r as isObjectType,o as messageTimeout};
|
package/dist/types/types.d.ts
CHANGED
|
@@ -4,12 +4,18 @@ export declare enum IMCMessageTypeEnum {
|
|
|
4
4
|
Fetch = "fetch",
|
|
5
5
|
Notification = "notification",
|
|
6
6
|
ThemeChange = "theme-change",
|
|
7
|
-
InstallAgent = "install-agent",
|
|
8
7
|
RunAgentMethod = "run-agent-method",
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
UseVAD = "use-vad",
|
|
9
|
+
UseSTT = "use-stt",
|
|
10
|
+
UseLLM = "use-llm",
|
|
11
|
+
UseTTS = "use-tts",
|
|
12
|
+
UseDiffusion = "use-diffusion",
|
|
13
|
+
UseOCR = "use-ocr",
|
|
14
|
+
UseSpeech2Speech = "use-speech2speech",
|
|
15
|
+
RunExtCommand = "run-ext-command",
|
|
11
16
|
RequestTerminal = "request-terminal",
|
|
12
|
-
|
|
17
|
+
ExtReady = "ext-ready",
|
|
18
|
+
ExtClose = "ext-close",
|
|
13
19
|
Loaded = "loaded",
|
|
14
20
|
Acknowledge = "acknowledge",
|
|
15
21
|
Abort = "abort",
|
|
@@ -21,19 +27,22 @@ export type IMCMessage = {
|
|
|
21
27
|
type: IMCMessageTypeEnum;
|
|
22
28
|
payload?: any;
|
|
23
29
|
};
|
|
24
|
-
export type
|
|
25
|
-
|
|
26
|
-
}>;
|
|
30
|
+
export type ReceiverHandler = (senderWindow: Window, message: IMCMessage, abortSignal?: AbortSignal) => Promise<any>;
|
|
31
|
+
export type ReceiverHandlerMap = Map<IMCMessageTypeEnum, ReceiverHandler>;
|
|
27
32
|
export type TextFileSelection = {
|
|
28
33
|
lineStart: number;
|
|
29
34
|
lineEnd: number;
|
|
30
35
|
text: string;
|
|
31
36
|
};
|
|
32
|
-
export type
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
export type ViewModel = {
|
|
38
|
+
viewId: string;
|
|
39
|
+
isFocused: boolean;
|
|
40
|
+
file?: {
|
|
41
|
+
content: string;
|
|
42
|
+
path: string;
|
|
43
|
+
selections?: TextFileSelection[];
|
|
44
|
+
};
|
|
45
|
+
extensionConfig?: ExtensionConfig;
|
|
37
46
|
};
|
|
38
47
|
export type FetchPayload = {
|
|
39
48
|
uri: string;
|
|
@@ -61,6 +70,8 @@ export type ExtensionConfig = {
|
|
|
61
70
|
fileTypes?: string[];
|
|
62
71
|
preview?: string;
|
|
63
72
|
enabledPlatforms?: Record<string, boolean>;
|
|
73
|
+
agents?: Agent[];
|
|
74
|
+
commandsInfoList?: ExtensionCommandInfo[];
|
|
64
75
|
};
|
|
65
76
|
export type Agent = {
|
|
66
77
|
name: string;
|
|
@@ -77,43 +88,62 @@ export type Agent = {
|
|
|
77
88
|
export type AgentMethod = {
|
|
78
89
|
access: AccessEnum;
|
|
79
90
|
name: string;
|
|
80
|
-
parameters: Record<string,
|
|
91
|
+
parameters: Record<string, TypedVariable>;
|
|
81
92
|
prompt: string;
|
|
82
|
-
returns: Record<string,
|
|
93
|
+
returns: Record<string, TypedVariable>;
|
|
83
94
|
LLMConfig?: LLMConfig;
|
|
84
95
|
};
|
|
85
|
-
export type
|
|
86
|
-
type:
|
|
96
|
+
export type TypedVariable = {
|
|
97
|
+
type: TypedVariableType;
|
|
98
|
+
name: string;
|
|
87
99
|
description: string;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
type AgentVariableTypeArray = {
|
|
91
|
-
size: number;
|
|
92
|
-
elementType: AgentVariableType;
|
|
100
|
+
optional?: boolean;
|
|
101
|
+
defaultValue?: any;
|
|
93
102
|
};
|
|
94
103
|
/**
|
|
95
104
|
* A tool that agent can use during method execution.
|
|
96
105
|
*
|
|
97
|
-
* This is linked to a callback function created by user,
|
|
98
|
-
* tool developer, or extension.
|
|
99
106
|
*
|
|
100
|
-
* The tool may optionally return a value to
|
|
101
|
-
* agent method.
|
|
107
|
+
* The tool may optionally return a value to the agent.
|
|
102
108
|
*/
|
|
103
109
|
export type AgentTool = {
|
|
104
110
|
access: AccessEnum;
|
|
105
111
|
name: string;
|
|
106
112
|
description: string;
|
|
107
|
-
parameters: Record<string,
|
|
108
|
-
returns: Record<string,
|
|
113
|
+
parameters: Record<string, TypedVariable>;
|
|
114
|
+
returns: Record<string, TypedVariable>;
|
|
109
115
|
};
|
|
110
|
-
export type
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
temperature: number;
|
|
116
|
+
export type TypedVariableType = "string" | "number" | "boolean" | TypedVariableObjectType | TypedVariableArrayType;
|
|
117
|
+
export type TypedVariableObjectType = {
|
|
118
|
+
[key: string]: TypedVariable;
|
|
114
119
|
};
|
|
120
|
+
export type TypedVariableArrayType = [TypedVariableType];
|
|
121
|
+
export declare function isArrayType(value: TypedVariableType): value is TypedVariableArrayType;
|
|
122
|
+
export declare function isObjectType(value: TypedVariableType): value is TypedVariableObjectType;
|
|
115
123
|
export declare enum AccessEnum {
|
|
116
124
|
public = "public",
|
|
117
125
|
private = "private"
|
|
118
126
|
}
|
|
119
|
-
export {
|
|
127
|
+
export type STTConfig = {
|
|
128
|
+
provider: string;
|
|
129
|
+
modelName: string;
|
|
130
|
+
};
|
|
131
|
+
export type LLMConfig = {
|
|
132
|
+
provider: string;
|
|
133
|
+
modelName: string;
|
|
134
|
+
temperature: number;
|
|
135
|
+
};
|
|
136
|
+
export type TTSConfig = {
|
|
137
|
+
provider: string;
|
|
138
|
+
modelName: string;
|
|
139
|
+
voice: string;
|
|
140
|
+
};
|
|
141
|
+
export type ExtensionCommandInfo = {
|
|
142
|
+
name: string;
|
|
143
|
+
description: string;
|
|
144
|
+
parameters: Record<string, TypedVariable>;
|
|
145
|
+
};
|
|
146
|
+
export type ExtensionCommand = {
|
|
147
|
+
info: ExtensionCommandInfo;
|
|
148
|
+
handler: (args: any) => Promise<any>;
|
|
149
|
+
};
|