@@ -21,10 +21,12 @@ import useIMC from "../../lib/use-imc";
2121 *
2222 */
2323export default function useRegisterAction (
24- name : string ,
25- description : string ,
26- parameters : Record < string , TypedVariable > ,
27- returns : Record < string , TypedVariable > ,
24+ actionInfo : {
25+ name : string ;
26+ description : string ;
27+ parameters ?: Record < string , TypedVariable > ;
28+ returns ?: Record < string , TypedVariable > ;
29+ } ,
2830 callbackHandler ?: ( args : any ) => Promise < string | void > ,
2931 isExtReady : boolean = true
3032) {
@@ -34,10 +36,10 @@ export default function useRegisterAction(
3436 const commandQueue = useRef < { args : any ; resolve : ( v : any ) => void } [ ] > ( [ ] ) ;
3537
3638 const [ action , setAction ] = useState < Action > ( {
37- name,
38- description,
39- parameters,
40- returns,
39+ name : actionInfo . name ,
40+ description : actionInfo . description ,
41+ parameters : actionInfo . parameters ?? { } ,
42+ returns : actionInfo . returns ?? { } ,
4143 handler : callbackHandler ,
4244 } ) ;
4345
@@ -76,7 +78,13 @@ export default function useRegisterAction(
7678 } , [ action , imc , isExtReady ] ) ;
7779
7880 useEffect ( ( ) => {
79- setAction ( ( prev ) => ( { ...prev , name, description, parameters, returns } ) ) ;
81+ setAction ( ( prev ) => ( {
82+ ...prev ,
83+ name : actionInfo . name ,
84+ description : actionInfo . description ,
85+ parameters : actionInfo . parameters ?? { } ,
86+ returns : actionInfo . returns ?? { } ,
87+ } ) ) ;
8088 } , [ callbackHandler ] ) ;
8189
8290 async function executeAction ( args : any ) {
@@ -94,9 +102,9 @@ export default function useRegisterAction(
94102 const { name : requestedName , args } : { name : string ; args : any } =
95103 message . payload ;
96104
97- if ( name === requestedName ) {
105+ if ( actionInfo . name === requestedName ) {
98106 // Validate parameters
99- const actionParams = parameters ;
107+ const actionParams = actionInfo . parameters ?? { } ;
100108 if ( Object . keys ( args ) . length !== Object . keys ( actionParams ) . length ) {
101109 throw new Error (
102110 `Invalid number of parameters: expected ${
@@ -106,13 +114,13 @@ export default function useRegisterAction(
106114 }
107115
108116 for ( const [ key , value ] of Object . entries ( args ) ) {
109- if ( parameters [ key ] === undefined ) {
117+ if ( actionParams [ key ] === undefined ) {
110118 throw new Error ( `Invalid parameter: ${ key } ` ) ;
111119 }
112- if ( typeof value !== parameters [ key ] . type ) {
120+ if ( typeof value !== actionParams [ key ] . type ) {
113121 throw new Error (
114122 `Invalid type for parameter ${ key } : expected ${
115- parameters [ key ] . type
123+ actionParams [ key ] . type
116124 } , got ${ typeof value } . Value received: ${ value } `
117125 ) ;
118126 }
0 commit comments