@@ -208,9 +208,8 @@ defmodule Codex.AppServer do
208208 def request_typed ( conn , method , params , response_module , opts \\ [ ] )
209209 when is_pid ( conn ) and is_binary ( method ) and is_atom ( response_module ) and is_list ( opts ) do
210210 with { :ok , encoded_params } <- encode_typed_request_params ( method , params ) ,
211- { :ok , result } <- Connection . request ( conn , method , encoded_params , opts ) ,
212- { :ok , typed_response } <- parse_typed_response ( response_module , result ) do
213- { :ok , typed_response }
211+ { :ok , result } <- Connection . request ( conn , method , encoded_params , opts ) do
212+ parse_typed_response ( response_module , result )
214213 end
215214 end
216215
@@ -891,12 +890,14 @@ defmodule Codex.AppServer do
891890 """
892891 @ spec plugin_list ( connection ( ) , keyword ( ) ) :: { :ok , map ( ) } | { :error , term ( ) }
893892 def plugin_list ( conn , opts \\ [ ] ) when is_pid ( conn ) and is_list ( opts ) do
894- params = % Plugin.ListParams {
895- cwds: Keyword . get ( opts , :cwds ) ,
896- force_remote_sync: Plugin.Helpers . raw_true? ( Keyword . get ( opts , :force_remote_sync ) )
897- }
898-
899- Connection . request ( conn , "plugin/list" , Plugin.ListParams . to_map ( params ) , timeout_ms: 30_000 )
893+ with { :ok , params } <-
894+ encode_plugin_request_params (
895+ "plugin/list" ,
896+ cwds: Keyword . get ( opts , :cwds ) ,
897+ force_remote_sync: Keyword . get ( opts , :force_remote_sync )
898+ ) do
899+ Connection . request ( conn , "plugin/list" , params , timeout_ms: 30_000 )
900+ end
900901 end
901902
902903 @ doc """
@@ -922,18 +923,15 @@ defmodule Codex.AppServer do
922923 def plugin_install ( conn , marketplace_path , plugin_name , opts \\ [ ] )
923924 when is_pid ( conn ) and is_binary ( marketplace_path ) and is_binary ( plugin_name ) and
924925 is_list ( opts ) do
925- params = % Plugin.InstallParams {
926- marketplace_path: marketplace_path ,
927- plugin_name: plugin_name ,
928- force_remote_sync: Plugin.Helpers . raw_true? ( Keyword . get ( opts , :force_remote_sync ) )
929- }
930-
931- Connection . request (
932- conn ,
933- "plugin/install" ,
934- Plugin.InstallParams . to_map ( params ) ,
935- timeout_ms: 30_000
936- )
926+ with { :ok , params } <-
927+ encode_plugin_request_params (
928+ "plugin/install" ,
929+ marketplace_path: marketplace_path ,
930+ plugin_name: plugin_name ,
931+ force_remote_sync: Keyword . get ( opts , :force_remote_sync )
932+ ) do
933+ Connection . request ( conn , "plugin/install" , params , timeout_ms: 30_000 )
934+ end
937935 end
938936
939937 @ doc """
@@ -965,14 +963,14 @@ defmodule Codex.AppServer do
965963 @ spec plugin_read ( connection ( ) , String . t ( ) , String . t ( ) ) :: { :ok , map ( ) } | { :error , term ( ) }
966964 def plugin_read ( conn , marketplace_path , plugin_name )
967965 when is_pid ( conn ) and is_binary ( marketplace_path ) and is_binary ( plugin_name ) do
968- params = % Plugin.ReadParams { marketplace_path: marketplace_path , plugin_name: plugin_name }
969-
970- Connection . request (
971- conn ,
972- "plugin/read" ,
973- Plugin.ReadParams . to_map ( params ) ,
974- timeout_ms: 30_000
975- )
966+ with { :ok , params } <-
967+ encode_plugin_request_params (
968+ "plugin/read" ,
969+ marketplace_path: marketplace_path ,
970+ plugin_name: plugin_name
971+ ) do
972+ Connection . request ( conn , "plugin/read" , params , timeout_ms: 30_000 )
973+ end
976974 end
977975
978976 @ doc """
@@ -1003,17 +1001,14 @@ defmodule Codex.AppServer do
10031001 @ spec plugin_uninstall ( connection ( ) , String . t ( ) , keyword ( ) ) :: { :ok , map ( ) } | { :error , term ( ) }
10041002 def plugin_uninstall ( conn , plugin_id , opts \\ [ ] )
10051003 when is_pid ( conn ) and is_binary ( plugin_id ) and is_list ( opts ) do
1006- params = % Plugin.UninstallParams {
1007- plugin_id: plugin_id ,
1008- force_remote_sync: Plugin.Helpers . raw_true? ( Keyword . get ( opts , :force_remote_sync ) )
1009- }
1010-
1011- Connection . request (
1012- conn ,
1013- "plugin/uninstall" ,
1014- Plugin.UninstallParams . to_map ( params ) ,
1015- timeout_ms: 30_000
1016- )
1004+ with { :ok , params } <-
1005+ encode_plugin_request_params (
1006+ "plugin/uninstall" ,
1007+ plugin_id: plugin_id ,
1008+ force_remote_sync: Keyword . get ( opts , :force_remote_sync )
1009+ ) do
1010+ Connection . request ( conn , "plugin/uninstall" , params , timeout_ms: 30_000 )
1011+ end
10171012 end
10181013
10191014 @ doc """
@@ -1327,14 +1322,23 @@ defmodule Codex.AppServer do
13271322 defp encode_command_exec_delta ( delta ) when is_binary ( delta ) , do: Base . encode64 ( delta )
13281323 defp encode_command_exec_delta ( delta ) , do: delta
13291324
1325+ defp encode_plugin_request_params ( method , params ) when is_binary ( method ) and is_list ( params ) do
1326+ params
1327+ |> normalize_plugin_request_params ( method )
1328+ |> then ( & encode_typed_request_params ( method , & 1 ) )
1329+ end
1330+
13301331 defp encode_typed_request_params ( method , nil ) do
13311332 case typed_params_module ( method ) do
13321333 nil ->
13331334 { :ok , % { } }
13341335
1335- module when is_atom ( module ) ->
1336- with { :ok , typed_params } <- apply ( module , :parse , [ % { } ] ) do
1337- { :ok , apply ( module , :to_map , [ typed_params ] ) }
1336+ module when is_atom ( module ) and not is_nil ( module ) ->
1337+ parser = :erlang . make_fun ( module , :parse , 1 )
1338+ encoder = :erlang . make_fun ( module , :to_map , 1 )
1339+
1340+ with { :ok , typed_params } <- parser . ( % { } ) do
1341+ { :ok , encoder . ( typed_params ) }
13381342 end
13391343 end
13401344 end
@@ -1352,9 +1356,12 @@ defmodule Codex.AppServer do
13521356 nil ->
13531357 { :ok , Params . normalize_map ( params ) }
13541358
1355- module when is_atom ( module ) ->
1356- with { :ok , typed_params } <- apply ( module , :parse , [ params ] ) do
1357- { :ok , apply ( module , :to_map , [ typed_params ] ) }
1359+ module when is_atom ( module ) and not is_nil ( module ) ->
1360+ parser = :erlang . make_fun ( module , :parse , 1 )
1361+ encoder = :erlang . make_fun ( module , :to_map , 1 )
1362+
1363+ with { :ok , typed_params } <- parser . ( params ) do
1364+ { :ok , encoder . ( typed_params ) }
13581365 end
13591366 end
13601367 end
@@ -1375,6 +1382,9 @@ defmodule Codex.AppServer do
13751382 { :error , { :invalid_typed_response_module , response_module } }
13761383 end
13771384 rescue
1385+ error in [ CliSubprocessCore.Schema.Error ] ->
1386+ { :error , { error . tag , error . details } }
1387+
13781388 error in [ ArgumentError , RuntimeError ] ->
13791389 { :error , { :invalid_typed_response , response_module , error } }
13801390 end
@@ -1385,6 +1395,17 @@ defmodule Codex.AppServer do
13851395 defp typed_params_module ( "plugin/uninstall" ) , do: Plugin.UninstallParams
13861396 defp typed_params_module ( _method ) , do: nil
13871397
1398+ defp normalize_plugin_request_params ( params , method )
1399+ when method in [ "plugin/list" , "plugin/install" , "plugin/uninstall" ] and is_list ( params ) do
1400+ if Keyword . has_key? ( params , :force_remote_sync ) do
1401+ Keyword . update! ( params , :force_remote_sync , & Plugin.Helpers . raw_true? / 1 )
1402+ else
1403+ params
1404+ end
1405+ end
1406+
1407+ defp normalize_plugin_request_params ( params , _method ) when is_list ( params ) , do: params
1408+
13881409 defp normalize_true ( true ) , do: true
13891410 defp normalize_true ( "true" ) , do: true
13901411 defp normalize_true ( _ ) , do: nil
0 commit comments