@pulse-editor/react-api 0.1.1-alpha.43 → 0.1.1-alpha.45

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.
@@ -1,4 +1,5 @@
1
1
  import { TypedVariable } from "@pulse-editor/shared-utils";
2
+ import { DependencyList } from "react";
2
3
  /**
3
4
  * Register an app action to listen to IMC messages from the core,
4
5
  * and pass to the action to handle.
@@ -8,7 +9,9 @@ import { TypedVariable } from "@pulse-editor/shared-utils";
8
9
  * @param parameters Parameters of the command.
9
10
  * @param returns Return values of the command.
10
11
  * @param callbackHandler Callback handler function to handle the command.
12
+ * @param deps Dependency list to re-register the action when changed.
11
13
  * @param isExtReady Whether the extension is ready to receive commands.
14
+ * Useful for actions that need to wait for some certain app state to be ready.
12
15
  *
13
16
  */
14
17
  export default function useRegisterAction(actionInfo: {
@@ -16,6 +19,6 @@ export default function useRegisterAction(actionInfo: {
16
19
  description: string;
17
20
  parameters?: Record<string, TypedVariable>;
18
21
  returns?: Record<string, TypedVariable>;
19
- }, callbackHandler?: (args: any) => Promise<string | void>, isExtReady?: boolean): {
22
+ }, callbackHandler: (args: any) => Promise<any>, deps: DependencyList, isExtReady?: boolean): {
20
23
  isReady: boolean;
21
24
  };
package/dist/main.js CHANGED
@@ -1 +1 @@
1
- import{InterModuleCommunication as e,IMCMessageTypeEnum as n}from"@pulse-editor/shared-utils";import{useState as t,useEffect as r,useRef as i}from"react";function o(i){const[o,a]=t(void 0),[s,d]=t(!1),[c,u]=t(!1),p=window.parent;return r(()=>(u(!0),()=>{u(!1),o?.close(),a(void 0)}),[]),r(()=>{!async function(){if(!c)return;if(void 0!==o)return;const t=new e;t.initThisWindow(window),t.updateReceiverHandlerMap(i),await t.initOtherWindow(p),a(t),t.sendMessage(n.AppReady).then(()=>{d(!0)})}()},[c,o]),{imc:o,isReady:s}}function a(){return o(new Map),{}}function s(){const e=new Map,{imc:t,isReady:r}=o(e);return{runAgentMethod:async function(e,r,i,o,a){if(!t)throw new Error("IMC not initialized.");return await t.sendMessage(n.EditorRunAgentMethod,{agentName:e,methodName:r,parameters:i,llmConfig:a},o).then(e=>e)},isReady:r}}function d(e,a,s=!0){const{isReady:d,imc:c}=o(w()),u=i([]),[p,f]=t({name:e.name,description:e.description,parameters:e.parameters??{},returns:e.returns??{},handler:a});async function m(e){if(!p.handler)return;return await p.handler(e)}function w(){return new Map([[n.EditorRunAppAction,async(n,t)=>{const{name:r,args:i}=t.payload;if(e.name===r){const n=e.parameters??{};if(Object.keys(i).length!==Object.keys(n).length)throw new Error(`Invalid number of parameters: expected ${Object.keys(n).length}, got ${Object.keys(i).length}`);for(const[e,t]of Object.entries(i)){if(void 0===n[e])throw new Error(`Invalid parameter: ${e}`);if(typeof t!==n[e].type)throw new Error(`Invalid type for parameter ${e}: expected ${n[e].type}, got ${typeof t}. Value received: ${t}`)}return s?await m(i):new Promise(e=>{u.current.push({args:i,resolve:e})})}}]])}return r(()=>{if(s&&u.current.length>0){const e=[...u.current];u.current=[],e.forEach(async({args:e,resolve:n})=>{n(await m(e))})}},[s]),r(()=>{s&&async function(){await(c?.sendMessage(n.EditorRegisterAction,{name:p.name,description:p.description,parameters:p.parameters,returns:p.returns})),c?.updateReceiverHandlerMap(w())}()},[p,c,s]),r(()=>{f(n=>({...n,name:e.name,description:e.description,parameters:e.parameters??{},returns:e.returns??{}}))},[a]),{isReady:d}}function c(){const[e,i]=t(void 0),a=new Map,{imc:s,isReady:d}=o(a);return r(()=>{d&&s?.sendMessage(n.PlatformReadFile).then(e=>{i(e)})},[d]),{viewModel:e,updateViewModel:function(e){s?.sendMessage(n.PlatformWriteFile,e)}}}function u(){const e=new Map,{imc:i,isReady:a}=o(e),[s,d]=t(!0);return r(()=>{a&&i?.sendMessage(n.EditorLoadingApp,{isLoading:s})},[s]),{isReady:a,toggleLoading:function(e){d(n=>e)}}}function p(){const e=new Map,{imc:t}=o(e);return{openNotification:function(e,r){if(!t)throw new Error("IMC is not initialized.");t.sendMessage(n.EditorShowNotification,{text:e,type:r})}}}function f(){const[e,r]=t("light"),i=new Map;return i.set(n.EditorThemeUpdate,async(e,n)=>{const t=n.payload;r(e=>t)}),o(i),{theme:e}}function m(){return{}}function w(){const e=new Map,{imc:t,isReady:r}=o(e);return{runImageGen:async function(e,r,i){if(!t)throw new Error("IMC not initialized.");if(!e&&!r)throw new Error("At least one of textPrompt or imagePrompt is required.");return await t.sendMessage(n.ModalityImageGen,{textPrompt:e,imagePrompt:r,imageModelConfig:i}).then(e=>e)},isReady:r}}function l(){const e=new Map,{imc:t,isReady:r}=o(e);return{runLLM:async function(e,r){if(!t)throw new Error("IMC not initialized.");return await t.sendMessage(n.ModalityLLM,{prompt:e,llmConfig:r}).then(e=>e)},isReady:r}}function y(){const e=new Map,{imc:t}=o(e);return{recognizeText:async function(e){if(!t)throw new Error("IMC is not initialized.");return(await t.sendMessage(n.ModalityOCR,{image:e})).payload.text}}}function M(){const e=new Map,{imc:t,isReady:r}=o(e);return{runSTT:async function(e,r){if(!t)throw new Error("IMC not initialized.");return await t.sendMessage(n.ModalitySTT,{audio:e,sttConfig:r}).then(e=>e)},isReady:r}}function g(){const e=new Map,{imc:t,isReady:r}=o(e);return{runTTS:async function(e,r){if(!t)throw new Error("IMC not initialized.");return await t.sendMessage(n.ModalityTTS,{text:e,ttsConfig:r}).then(e=>e)},isReady:r}}function h(){const e=new Map,{imc:t,isReady:r}=o(e);return{runVideoGen:async function(e,r,i,o){if(!t)throw new Error("IMC not initialized.");if(!r&&!i)throw new Error("At least one of textPrompt or imagePrompt is required.");return await t.sendMessage(n.ModalityVideoGen,{duration:e,textPrompt:r,imagePrompt:i,videoModelConfig:o}).then(e=>e)},isReady:r}}function R(){const e=new Map,{imc:i,isReady:a}=o(e),[s,d]=t({});return r(()=>{a&&i?.sendMessage(n.EditorGetEnv).then(e=>{d(e)})},[a]),{isReady:a,envs:s}}function E(){const e=new Map,{imc:i,isReady:a}=o(e),[s,d]=t(void 0),[c,u]=t(void 0);return r(()=>{a&&i?.sendMessage(n.PlatformCreateTerminal).then(e=>{const{websocketUrl:n,projectHomePath:t}=e;d(n),u(t)})},[a]),{websocketUrl:s,projectHomePath:c}}export{a as useAgentTools,s as useAgents,c as useFileView,w as useImageGen,l as useLLM,u as useLoading,p as useNotification,y as useOCR,R as usePulseEnv,d as useRegisterAction,M as useSTT,g as useTTS,E as useTerminal,f as useTheme,m as useToolbar,h as useVideoGen};
1
+ import{InterModuleCommunication as e,IMCMessageTypeEnum as n}from"@pulse-editor/shared-utils";import{useState as t,useEffect as r,useRef as i}from"react";function o(i){const[o,a]=t(void 0),[s,d]=t(!1),[c,u]=t(!1),p=window.parent;return r(()=>(u(!0),()=>{u(!1),o?.close(),a(void 0)}),[]),r(()=>{!async function(){if(!c)return;if(void 0!==o)return;const t=new e;t.initThisWindow(window),t.updateReceiverHandlerMap(i),await t.initOtherWindow(p),a(t),t.sendMessage(n.AppReady).then(()=>{d(!0)})}()},[c,o]),{imc:o,isReady:s}}function a(){return o(new Map),{}}function s(){const e=new Map,{imc:t,isReady:r}=o(e);return{runAgentMethod:async function(e,r,i,o,a){if(!t)throw new Error("IMC not initialized.");return await t.sendMessage(n.EditorRunAgentMethod,{agentName:e,methodName:r,parameters:i,llmConfig:a},o).then(e=>e)},isReady:r}}function d(e,a,s,d=!0){const{isReady:c,imc:u}=o(l()),p=i([]),[f,m]=t({name:e.name,description:e.description,parameters:e.parameters??{},returns:e.returns??{},handler:a});async function w(e){if(!f.handler)return;return await f.handler(e)}function l(){return new Map([[n.EditorRunAppAction,async(n,t)=>{const{name:r,args:i}=t.payload;if(e.name===r){const n=e.parameters??{};if(Object.keys(i).length!==Object.keys(n).length)throw new Error(`Invalid number of parameters: expected ${Object.keys(n).length}, got ${Object.keys(i).length}`);for(const[e,t]of Object.entries(i)){if(void 0===n[e])throw new Error(`Invalid parameter: ${e}`);if(typeof t!==n[e].type)throw new Error(`Invalid type for parameter ${e}: expected ${n[e].type}, got ${typeof t}. Value received: ${t}`)}return d?await w(i):new Promise(e=>{p.current.push({args:i,resolve:e})})}}]])}return r(()=>{if(d&&p.current.length>0){const e=[...p.current];p.current=[],e.forEach(async({args:e,resolve:n})=>{n(await w(e))})}},[d]),r(()=>{d&&async function(){await(u?.sendMessage(n.EditorRegisterAction,{name:f.name,description:f.description,parameters:f.parameters,returns:f.returns})),u?.updateReceiverHandlerMap(l())}()},[f,u,d]),r(()=>{m(n=>({...n,name:e.name,description:e.description,parameters:e.parameters??{},returns:e.returns??{},handler:a}))},[...s]),{isReady:c}}function c(){const[e,i]=t(void 0),a=new Map,{imc:s,isReady:d}=o(a);return r(()=>{d&&s?.sendMessage(n.PlatformReadFile).then(e=>{i(e)})},[d]),{viewModel:e,updateViewModel:function(e){s?.sendMessage(n.PlatformWriteFile,e)}}}function u(){const e=new Map,{imc:i,isReady:a}=o(e),[s,d]=t(!0);return r(()=>{a&&i?.sendMessage(n.EditorLoadingApp,{isLoading:s})},[s]),{isReady:a,toggleLoading:function(e){d(n=>e)}}}function p(){const e=new Map,{imc:t}=o(e);return{openNotification:function(e,r){if(!t)throw new Error("IMC is not initialized.");t.sendMessage(n.EditorShowNotification,{text:e,type:r})}}}function f(){const[e,r]=t("light"),i=new Map;return i.set(n.EditorThemeUpdate,async(e,n)=>{const t=n.payload;r(e=>t)}),o(i),{theme:e}}function m(){return{}}function w(){const e=new Map,{imc:t,isReady:r}=o(e);return{runImageGen:async function(e,r,i){if(!t)throw new Error("IMC not initialized.");if(!e&&!r)throw new Error("At least one of textPrompt or imagePrompt is required.");return await t.sendMessage(n.ModalityImageGen,{textPrompt:e,imagePrompt:r,imageModelConfig:i}).then(e=>e)},isReady:r}}function l(){const e=new Map,{imc:t,isReady:r}=o(e);return{runLLM:async function(e,r){if(!t)throw new Error("IMC not initialized.");return await t.sendMessage(n.ModalityLLM,{prompt:e,llmConfig:r}).then(e=>e)},isReady:r}}function y(){const e=new Map,{imc:t}=o(e);return{recognizeText:async function(e){if(!t)throw new Error("IMC is not initialized.");return(await t.sendMessage(n.ModalityOCR,{image:e})).payload.text}}}function M(){const e=new Map,{imc:t,isReady:r}=o(e);return{runSTT:async function(e,r){if(!t)throw new Error("IMC not initialized.");return await t.sendMessage(n.ModalitySTT,{audio:e,sttConfig:r}).then(e=>e)},isReady:r}}function g(){const e=new Map,{imc:t,isReady:r}=o(e);return{runTTS:async function(e,r){if(!t)throw new Error("IMC not initialized.");return await t.sendMessage(n.ModalityTTS,{text:e,ttsConfig:r}).then(e=>e)},isReady:r}}function h(){const e=new Map,{imc:t,isReady:r}=o(e);return{runVideoGen:async function(e,r,i,o){if(!t)throw new Error("IMC not initialized.");if(!r&&!i)throw new Error("At least one of textPrompt or imagePrompt is required.");return await t.sendMessage(n.ModalityVideoGen,{duration:e,textPrompt:r,imagePrompt:i,videoModelConfig:o}).then(e=>e)},isReady:r}}function R(){const e=new Map,{imc:i,isReady:a}=o(e),[s,d]=t({});return r(()=>{a&&i?.sendMessage(n.EditorGetEnv).then(e=>{d(e)})},[a]),{isReady:a,envs:s}}function E(){const e=new Map,{imc:i,isReady:a}=o(e),[s,d]=t(void 0),[c,u]=t(void 0);return r(()=>{a&&i?.sendMessage(n.PlatformCreateTerminal).then(e=>{const{websocketUrl:n,projectHomePath:t}=e;d(n),u(t)})},[a]),{websocketUrl:s,projectHomePath:c}}export{a as useAgentTools,s as useAgents,c as useFileView,w as useImageGen,l as useLLM,u as useLoading,p as useNotification,y as useOCR,R as usePulseEnv,d as useRegisterAction,M as useSTT,g as useTTS,E as useTerminal,f as useTheme,m as useToolbar,h as useVideoGen};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pulse-editor/react-api",
3
- "version": "0.1.1-alpha.43",
3
+ "version": "0.1.1-alpha.45",
4
4
  "main": "dist/main.js",
5
5
  "files": [
6
6
  "dist"
@@ -38,7 +38,7 @@
38
38
  "typescript-eslint": "^8.30.1"
39
39
  },
40
40
  "peerDependencies": {
41
- "@pulse-editor/shared-utils": "0.1.1-alpha.43",
41
+ "@pulse-editor/shared-utils": "0.1.1-alpha.45",
42
42
  "react": "^19.0.0",
43
43
  "react-dom": "^19.0.0"
44
44
  }