@pulse-editor/shared-utils 0.1.1-alpha.33 → 0.1.1-alpha.35

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/main.js CHANGED
@@ -1 +1 @@
1
- var e,n,i,t,s;function r(e){return Array.isArray(e)&&1===e.length}function o(e){return"object"==typeof e&&!Array.isArray(e)}!function(e){e.ModalityVAD="modality-vad",e.ModalitySTT="modality-stt",e.ModalityLLM="modality-llm",e.ModalityTTS="modality-tts",e.ModalitySpeech2Speech="modality-speech-to-speech",e.ModalityImageGen="modality-image-gen",e.ModalityVideoGen="modality-video-gen",e.ModalityOCR="modality-ocr",e.ModalityMusicGen="modality-music-gen",e.ExtReady="ext-ready",e.ExtClose="ext-close",e.EditorLoadingExt="editor-loading-ext",e.EditorRunExtCommand="editor-run-ext-command",e.EditorRunAgentMethod="editor-run-agent-method",e.EditorThemeUpdate="editor-theme-update",e.EditorShowNotification="editor-show-notification",e.PlatformCreateTerminal="platform-create-terminal",e.PlatformWriteFile="platform-write-file",e.PlatformReadFile="platform-read-file",e.SignalGetWindowId="signal-get-window-id",e.SignalReturnWindowId="signal-return-window-id",e.SignalAcknowledge="signal-acknowledge",e.SignalAbort="signal-abort",e.SignalError="signal-error"}(e||(e={})),function(e){e.App="app",e.Canvas="canvas",e.Home="home"}(n||(n={})),function(e){e.Success="success",e.Error="error",e.Info="info",e.Warning="warning"}(i||(i={})),function(e){e.Generic="generic",e.FileView="file-view",e.ConsoleView="console-view"}(t||(t={})),function(e){e.public="public",e.private="private"}(s||(s={}));const a=3e5;class d{constructor(e,n){this.handlerMap=e,this.pendingTasks=new Map,this.windowId=n}receiveMessage(n,i){if(this.windowId===i.from)return;if(i.type===e.SignalAbort){const e=i.id,n=this.pendingTasks.get(e);return void(n&&(console.log("Aborting task",e),n.controller.abort(),this.pendingTasks.delete(e)))}const t=this.handlerMap.get(i.type);if(t){const s=new AbortController,r=s.signal;this.pendingTasks.set(i.id,{controller:s});t(n,i,r).then(t=>{r.aborted||i.type!==e.SignalAcknowledge&&this.acknowledgeSender(n,i.id,t)}).catch(t=>{const s={id:i.id,type:e.SignalError,payload:t.message,from:this.windowId};console.error("Error handling message:",t),n.postMessage(s,"*")}).finally(()=>{this.pendingTasks.delete(i.id)})}}acknowledgeSender(n,i,t){const s={id:i,type:e.SignalAcknowledge,payload:t,from:this.windowId};n.postMessage(s,"*")}}class l{constructor(e,n,i){this.targetWindow=e,this.timeout=n,this.pendingMessages=new Map,this.moduleId=i}async sendMessage(n,i,t){const s=(new Date).getTime().toString(),r={id:s,type:n,payload:i,from:this.moduleId};return new Promise((n,i)=>{if(t?.aborted)return i(new Error("Request aborted"));const o=()=>{this.pendingMessages.delete(s),this.targetWindow.postMessage({id:s,type:e.SignalAbort,payload:JSON.stringify({status:"Task aborted",data:null})},"*"),i(new Error("Request aborted"))};t?.addEventListener("abort",o),this.pendingMessages.set(s,{resolve:n,reject:i}),this.targetWindow.postMessage(r,"*");const a=setTimeout(()=>{this.pendingMessages.delete(s),t?.removeEventListener("abort",o),i(new Error("Communication with Pulse Editor timeout."))},this.timeout),d=this.pendingMessages.get(s);d&&(d.resolve=e=>{clearTimeout(a),t?.removeEventListener("abort",o),n(e)},d.reject=()=>{clearTimeout(a),t?.removeEventListener("abort",o),i()})})}getPendingMessage(e){return this.pendingMessages.get(e)}removePendingMessage(e){this.pendingMessages.delete(e)}}class h{initThisWindow(n,i){this.thisWindow=n;const t=n.viewId;if(!t)throw new Error("Current window's ID is not defined.");this.thisWindowId=t,this.receiverHandlerMap=new Map;const s=new d(this.receiverHandlerMap,this.thisWindowId);this.receiver=s,this.messageRecords=new Map,this.listener=n=>{const t=n.data.id,r=n.data.type;if(this.messageRecords?.has(t)&&r!==e.SignalGetWindowId)return void console.warn(`Duplicate message received with message ID: ${t}. Ignoring this message. Message: ${JSON.stringify(n.data)}`);if(this.messageRecords?.set(t,n.data),!s)throw new Error("Receiver not initialized at module "+this.thisWindowId);const o=n.data;if("development"===process.env.NODE_ENV&&void 0!==o.from&&console.log(`Module ${this.thisWindowId} received message from module ${o.from}:\n ${JSON.stringify(o)}`),i&&o.from!==i)return;const a=n.source;s.receiveMessage(a,o)},n.addEventListener("message",this.listener),console.log("Adding IMC listener in "+this.thisWindowId),this.setBaseHandler()}async initOtherWindow(n){return new Promise(i=>{if(!this.thisWindow||!this.thisWindowId)throw new Error("You must initialize the current window first.");this.thisWindow.addEventListener("message",e=>{if(!this.thisWindow||!this.thisWindowId)throw new Error("You must initialize the current window first.");const t=e.data.windowId;this.otherWindowId=t;const s=new l(n,a,this.thisWindowId);if(this.sender=s,!this.receiverHandlerMap)throw new Error("You must initialize the current window first.");this.setBaseHandler(),i()},{once:!0}),this.otherWindow=n,this.otherWindow.postMessage({type:e.SignalGetWindowId,from:this.thisWindowId},"*")})}close(){this.listener&&window.removeEventListener("message",this.listener)}async sendMessage(e,n,i){const t=this.sender;if(!t)throw new Error("Sender not initialized");return await t.sendMessage(e,n,i)}updateReceiverHandlerMap(e){if(!this.receiver)throw new Error("Receiver not initialized");this.receiverHandlerMap?.clear(),this.setBaseHandler(),e.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}setBaseHandler(){this.receiverHandlerMap?.set(e.SignalAcknowledge,async(e,n)=>{const i=this.sender?.getPendingMessage(n.id);i&&(i.resolve(n.payload),this.sender?.removePendingMessage(n.id))}),this.receiverHandlerMap?.set(e.SignalGetWindowId,async(n,i)=>{console.log("Received window ID request. Sending current window ID to other window: ");const t=this.thisWindowId;if(!t)throw new Error("This window ID is not defined.");const s={id:i.id,type:e.SignalReturnWindowId,payload:{windowId:t},from:t};n.postMessage(s,"*")})}}class c{constructor(e){this.channels=new Map,this.baseReceiverHandlerMap=e,this.channelReceiverHandlerMapMap=new Map}async sendMessage(e,n,i,t){const s=this.channels.get(e);if(!s)throw new Error("Channel not found for window ID "+e);return await s.sendMessage(n,i,t)}updateBaseReceiverHandlerMap(e){this.baseReceiverHandlerMap=e,this.channels.forEach((e,n)=>{const i=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,this.channelReceiverHandlerMapMap.get(n));e.updateReceiverHandlerMap(i)})}updateChannelReceiverHandlerMap(e,n){const i=this.channels.get(e);if(!i)throw new Error("Channel not found for window ID "+e);const t=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,n);i.updateReceiverHandlerMap(t),this.channelReceiverHandlerMapMap.set(e,n)}async createChannel(e,n,i){console.log("Creating channel for window ID: "+n);const t=new h;t.initThisWindow(window,n),await t.initOtherWindow(e),this.channels.set(n,t),i?this.updateChannelReceiverHandlerMap(n,i):t.updateReceiverHandlerMap(this.baseReceiverHandlerMap)}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)}close(){this.channels.forEach(e=>{e.close()}),this.channels.clear(),this.channelReceiverHandlerMapMap.clear()}getCombinedHandlerMap(e,n){if(e&&n){return new Map([...e,...n])}return e||(n||new Map)}}class w{constructor(n,i,t){this.polyIMC=n,this.newConnectionReceiverHandlerMap=i,this.onConnection=t;const s=new h;this.listener=s,s.initThisWindow(window),s.updateReceiverHandlerMap(new Map([[e.ExtReady,async(e,n,i)=>{this.handleExtReady(e,n,i)}]]))}close(){this.listener?.close()}updateReceiverHandlerMap(e){this.newConnectionReceiverHandlerMap=e}async handleExtReady(e,n,i){const t=n.from;this.polyIMC.hasChannel(t)?console.log("Channel already exists for window ID "+t+". Re-using the existing channel."):(await this.polyIMC.createChannel(e,t,this.newConnectionReceiverHandlerMap),this.onConnection&&this.onConnection(e,n))}}export{s as AccessEnum,w as ConnectionListener,t as ExtensionTypeEnum,e as IMCMessageTypeEnum,h as InterModuleCommunication,d as MessageReceiver,l as MessageSender,i as NotificationTypeEnum,c as PolyIMC,n as ViewModeEnum,r as isArrayType,o as isObjectType,a as messageTimeout};
1
+ var e,n,i,t,s;function r(e){return Array.isArray(e)&&1===e.length}function o(e){return"object"==typeof e&&!Array.isArray(e)}!function(e){e.ModalityVAD="modality-vad",e.ModalitySTT="modality-stt",e.ModalityLLM="modality-llm",e.ModalityTTS="modality-tts",e.ModalitySpeech2Speech="modality-speech-to-speech",e.ModalityImageGen="modality-image-gen",e.ModalityVideoGen="modality-video-gen",e.ModalityOCR="modality-ocr",e.ModalityMusicGen="modality-music-gen",e.ExtReady="ext-ready",e.ExtClose="ext-close",e.EditorLoadingExt="editor-loading-ext",e.EditorRunExtCommand="editor-run-ext-command",e.EditorRunAgentMethod="editor-run-agent-method",e.EditorThemeUpdate="editor-theme-update",e.EditorShowNotification="editor-show-notification",e.EditorGetEnv="editor-get-env",e.PlatformCreateTerminal="platform-create-terminal",e.PlatformWriteFile="platform-write-file",e.PlatformReadFile="platform-read-file",e.SignalGetWindowId="signal-get-window-id",e.SignalReturnWindowId="signal-return-window-id",e.SignalAcknowledge="signal-acknowledge",e.SignalAbort="signal-abort",e.SignalError="signal-error"}(e||(e={})),function(e){e.App="app",e.Canvas="canvas",e.Home="home"}(n||(n={})),function(e){e.Success="success",e.Error="error",e.Info="info",e.Warning="warning"}(i||(i={})),function(e){e.Generic="generic",e.FileView="file-view",e.ConsoleView="console-view"}(t||(t={})),function(e){e.public="public",e.private="private"}(s||(s={}));const a=3e5;class d{constructor(e,n){this.handlerMap=e,this.pendingTasks=new Map,this.windowId=n}receiveMessage(n,i){if(this.windowId===i.from)return;if(i.type===e.SignalAbort){const e=i.id,n=this.pendingTasks.get(e);return void(n&&(console.log("Aborting task",e),n.controller.abort(),this.pendingTasks.delete(e)))}const t=this.handlerMap.get(i.type);if(t){const s=new AbortController,r=s.signal;this.pendingTasks.set(i.id,{controller:s});t(n,i,r).then(t=>{r.aborted||i.type!==e.SignalAcknowledge&&this.acknowledgeSender(n,i.id,t)}).catch(t=>{const s={id:i.id,type:e.SignalError,payload:t.message,from:this.windowId};console.error("Error handling message:",t),n.postMessage(s,"*")}).finally(()=>{this.pendingTasks.delete(i.id)})}}acknowledgeSender(n,i,t){const s={id:i,type:e.SignalAcknowledge,payload:t,from:this.windowId};n.postMessage(s,"*")}}class l{constructor(e,n,i){this.targetWindow=e,this.timeout=n,this.pendingMessages=new Map,this.moduleId=i}async sendMessage(n,i,t){const s=(new Date).getTime().toString(),r={id:s,type:n,payload:i,from:this.moduleId};return new Promise((n,i)=>{if(t?.aborted)return i(new Error("Request aborted"));const o=()=>{this.pendingMessages.delete(s),this.targetWindow.postMessage({id:s,type:e.SignalAbort,payload:JSON.stringify({status:"Task aborted",data:null})},"*"),i(new Error("Request aborted"))};t?.addEventListener("abort",o),this.pendingMessages.set(s,{resolve:n,reject:i}),this.targetWindow.postMessage(r,"*");const a=setTimeout(()=>{this.pendingMessages.delete(s),t?.removeEventListener("abort",o),i(new Error("Communication with Pulse Editor timeout."))},this.timeout),d=this.pendingMessages.get(s);d&&(d.resolve=e=>{clearTimeout(a),t?.removeEventListener("abort",o),n(e)},d.reject=()=>{clearTimeout(a),t?.removeEventListener("abort",o),i()})})}getPendingMessage(e){return this.pendingMessages.get(e)}removePendingMessage(e){this.pendingMessages.delete(e)}}class h{initThisWindow(n,i){this.thisWindow=n;const t=n.viewId;if(!t)throw new Error("Current window's ID is not defined.");this.thisWindowId=t,this.receiverHandlerMap=new Map;const s=new d(this.receiverHandlerMap,this.thisWindowId);this.receiver=s,this.messageRecords=new Map,this.listener=n=>{const t=n.data.id,r=n.data.type;if(this.messageRecords?.has(t)&&r!==e.SignalGetWindowId)return void console.warn(`Duplicate message received with message ID: ${t}. Ignoring this message. Message: ${JSON.stringify(n.data)}`);if(this.messageRecords?.set(t,n.data),!s)throw new Error("Receiver not initialized at module "+this.thisWindowId);const o=n.data;if("development"===process.env.NODE_ENV&&void 0!==o.from&&console.log(`Module ${this.thisWindowId} received message from module ${o.from}:\n ${JSON.stringify(o)}`),i&&o.from!==i)return;const a=n.source;s.receiveMessage(a,o)},n.addEventListener("message",this.listener),console.log("Adding IMC listener in "+this.thisWindowId),this.setBaseHandler()}async initOtherWindow(n){return new Promise(i=>{if(!this.thisWindow||!this.thisWindowId)throw new Error("You must initialize the current window first.");this.thisWindow.addEventListener("message",e=>{if(!this.thisWindow||!this.thisWindowId)throw new Error("You must initialize the current window first.");const t=e.data.windowId;this.otherWindowId=t;const s=new l(n,a,this.thisWindowId);if(this.sender=s,!this.receiverHandlerMap)throw new Error("You must initialize the current window first.");this.setBaseHandler(),i()},{once:!0}),this.otherWindow=n,this.otherWindow.postMessage({type:e.SignalGetWindowId,from:this.thisWindowId},"*")})}close(){this.listener&&window.removeEventListener("message",this.listener)}async sendMessage(e,n,i){const t=this.sender;if(!t)throw new Error("Sender not initialized");return await t.sendMessage(e,n,i)}updateReceiverHandlerMap(e){if(!this.receiver)throw new Error("Receiver not initialized");this.receiverHandlerMap?.clear(),this.setBaseHandler(),e.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}setBaseHandler(){this.receiverHandlerMap?.set(e.SignalAcknowledge,async(e,n)=>{const i=this.sender?.getPendingMessage(n.id);i&&(i.resolve(n.payload),this.sender?.removePendingMessage(n.id))}),this.receiverHandlerMap?.set(e.SignalGetWindowId,async(n,i)=>{console.log("Received window ID request. Sending current window ID to other window: ");const t=this.thisWindowId;if(!t)throw new Error("This window ID is not defined.");const s={id:i.id,type:e.SignalReturnWindowId,payload:{windowId:t},from:t};n.postMessage(s,"*")})}}class c{constructor(e){this.channels=new Map,this.baseReceiverHandlerMap=e,this.channelReceiverHandlerMapMap=new Map}async sendMessage(e,n,i,t){const s=this.channels.get(e);if(!s)throw new Error("Channel not found for window ID "+e);return await s.sendMessage(n,i,t)}updateBaseReceiverHandlerMap(e){this.baseReceiverHandlerMap=e,this.channels.forEach((e,n)=>{const i=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,this.channelReceiverHandlerMapMap.get(n));e.updateReceiverHandlerMap(i)})}updateChannelReceiverHandlerMap(e,n){const i=this.channels.get(e);if(!i)throw new Error("Channel not found for window ID "+e);const t=this.getCombinedHandlerMap(this.baseReceiverHandlerMap,n);i.updateReceiverHandlerMap(t),this.channelReceiverHandlerMapMap.set(e,n)}async createChannel(e,n,i){console.log("Creating channel for window ID: "+n);const t=new h;t.initThisWindow(window,n),await t.initOtherWindow(e),this.channels.set(n,t),i?this.updateChannelReceiverHandlerMap(n,i):t.updateReceiverHandlerMap(this.baseReceiverHandlerMap)}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)}close(){this.channels.forEach(e=>{e.close()}),this.channels.clear(),this.channelReceiverHandlerMapMap.clear()}getCombinedHandlerMap(e,n){if(e&&n){return new Map([...e,...n])}return e||(n||new Map)}}class w{constructor(n,i,t){this.polyIMC=n,this.newConnectionReceiverHandlerMap=i,this.onConnection=t;const s=new h;this.listener=s,s.initThisWindow(window),s.updateReceiverHandlerMap(new Map([[e.ExtReady,async(e,n,i)=>{this.handleExtReady(e,n,i)}]]))}close(){this.listener?.close()}updateReceiverHandlerMap(e){this.newConnectionReceiverHandlerMap=e}async handleExtReady(e,n,i){const t=n.from;this.polyIMC.hasChannel(t)?console.log("Channel already exists for window ID "+t+". Re-using the existing channel."):(await this.polyIMC.createChannel(e,t,this.newConnectionReceiverHandlerMap),this.onConnection&&this.onConnection(e,n))}}export{s as AccessEnum,w as ConnectionListener,t as ExtensionTypeEnum,e as IMCMessageTypeEnum,h as InterModuleCommunication,d as MessageReceiver,l as MessageSender,i as NotificationTypeEnum,c as PolyIMC,n as ViewModeEnum,r as isArrayType,o as isObjectType,a as messageTimeout};
@@ -15,6 +15,7 @@ export declare enum IMCMessageTypeEnum {
15
15
  EditorRunAgentMethod = "editor-run-agent-method",
16
16
  EditorThemeUpdate = "editor-theme-update",
17
17
  EditorShowNotification = "editor-show-notification",
18
+ EditorGetEnv = "editor-get-env",
18
19
  PlatformCreateTerminal = "platform-create-terminal",
19
20
  PlatformWriteFile = "platform-write-file",
20
21
  PlatformReadFile = "platform-read-file",
@@ -64,7 +65,6 @@ export declare enum ExtensionTypeEnum {
64
65
  export type ExtensionConfig = {
65
66
  id: string;
66
67
  version: string;
67
- mfVersion: string;
68
68
  author?: string;
69
69
  displayName?: string;
70
70
  description?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pulse-editor/shared-utils",
3
- "version": "0.1.1-alpha.33",
3
+ "version": "0.1.1-alpha.35",
4
4
  "main": "dist/main.js",
5
5
  "files": [
6
6
  "dist"