11import { useState , useEffect } from 'react' ;
22import { useOutletContext , useNavigate } from 'react-router-dom' ;
3- import { actionApi } from '../utils/api' ;
3+ import { actionApi , agentApi } from '../utils/api' ;
4+ import FormFieldDefinition from '../components/common/FormFieldDefinition' ;
45
56function ActionsPlayground ( ) {
67 const { showToast } = useOutletContext ( ) ;
@@ -12,6 +13,10 @@ function ActionsPlayground() {
1213 const [ result , setResult ] = useState ( null ) ;
1314 const [ loading , setLoading ] = useState ( false ) ;
1415 const [ loadingActions , setLoadingActions ] = useState ( true ) ;
16+ const [ actionMetadata , setActionMetadata ] = useState ( null ) ;
17+ const [ agentMetadata , setAgentMetadata ] = useState ( null ) ;
18+ const [ configFields , setConfigFields ] = useState ( [ ] ) ;
19+ const [ paramFields , setParamFields ] = useState ( [ ] ) ;
1520
1621 // Update document title
1722 useEffect ( ( ) => {
@@ -36,21 +41,86 @@ function ActionsPlayground() {
3641 } ;
3742
3843 fetchActions ( ) ;
39- } , [ showToast ] ) ;
44+ } , [ ] ) ;
45+
46+ // Fetch agent metadata on mount
47+ useEffect ( ( ) => {
48+ const fetchAgentMetadata = async ( ) => {
49+ try {
50+ const metadata = await agentApi . getAgentConfigMetadata ( ) ;
51+ setAgentMetadata ( metadata ) ;
52+ } catch ( err ) {
53+ console . error ( 'Error fetching agent metadata:' , err ) ;
54+ showToast ( 'Failed to load agent metadata' , 'error' ) ;
55+ }
56+ } ;
57+
58+ fetchAgentMetadata ( ) ;
59+ } , [ ] ) ;
60+
61+ // Fetch action definition when action is selected or config changes
62+ useEffect ( ( ) => {
63+ if ( ! selectedAction ) return ;
64+
65+ const fetchActionDefinition = async ( ) => {
66+ try {
67+ // Get config fields from agent metadata
68+ const actionMeta = agentMetadata ?. actions ?. find ( action => action . name === selectedAction ) ;
69+ const configFields = actionMeta ?. fields || [ ] ;
70+ setConfigFields ( configFields ) ;
71+
72+ // Parse current config to pass to action definition
73+ let currentConfig = { } ;
74+ try {
75+ currentConfig = JSON . parse ( configJson ) ;
76+ } catch ( err ) {
77+ console . error ( 'Error parsing current config:' , err ) ;
78+ }
79+
80+ // Get parameter fields from action definition
81+ const paramFields = await actionApi . getActionDefinition ( selectedAction , currentConfig ) ;
82+ setParamFields ( paramFields ) ;
83+
84+ // Reset JSON to match the new fields
85+ setConfigJson ( JSON . stringify ( currentConfig , null , 2 ) ) ;
86+ setParamsJson ( JSON . stringify ( { } , null , 2 ) ) ;
87+ setResult ( null ) ;
88+ } catch ( err ) {
89+ console . error ( 'Error fetching action definition:' , err ) ;
90+ showToast ( 'Failed to load action definition' , 'error' ) ;
91+ }
92+ } ;
93+
94+ fetchActionDefinition ( ) ;
95+ } , [ selectedAction , configJson , agentMetadata ] ) ;
4096
4197 // Handle action selection
4298 const handleActionChange = ( e ) => {
4399 setSelectedAction ( e . target . value ) ;
100+ setConfigJson ( '{}' ) ;
101+ setParamsJson ( '{}' ) ;
44102 setResult ( null ) ;
45103 } ;
46104
47- // Handle JSON input changes
48- const handleConfigChange = ( e ) => {
49- setConfigJson ( e . target . value ) ;
105+ // Handle form field changes
106+ const handleConfigChange = ( field , value ) => {
107+ try {
108+ const config = JSON . parse ( configJson ) ;
109+ config [ field ] = value ;
110+ setConfigJson ( JSON . stringify ( config , null , 2 ) ) ;
111+ } catch ( err ) {
112+ console . error ( 'Error updating config:' , err ) ;
113+ }
50114 } ;
51115
52- const handleParamsChange = ( e ) => {
53- setParamsJson ( e . target . value ) ;
116+ const handleParamsChange = ( field , value ) => {
117+ try {
118+ const params = JSON . parse ( paramsJson ) ;
119+ params [ field ] = value ;
120+ setParamsJson ( JSON . stringify ( params , null , 2 ) ) ;
121+ } catch ( err ) {
122+ console . error ( 'Error updating params:' , err ) ;
123+ }
54124 } ;
55125
56126 // Execute the selected action
@@ -138,31 +208,23 @@ function ActionsPlayground() {
138208 < h2 > Action Configuration</ h2 >
139209
140210 < form onSubmit = { handleExecuteAction } >
141- < div className = "form-group mb-6" >
142- < label htmlFor = "config-json" > Configuration (JSON):</ label >
143- < textarea
144- id = "config-json"
145- value = { configJson }
211+ { configFields . length > 0 && (
212+ < FormFieldDefinition
213+ fields = { configFields }
214+ values = { JSON . parse ( configJson ) }
146215 onChange = { handleConfigChange }
147- className = "form-control"
148- rows = "5"
149- placeholder = '{"key": "value"}'
216+ idPrefix = "config_"
150217 />
151- < p className = "text-xs text-gray-400 mt-1" > Enter JSON configuration for the action</ p >
152- </ div >
218+ ) }
153219
154- < div className = "form-group mb-6" >
155- < label htmlFor = "params-json" > Parameters (JSON):</ label >
156- < textarea
157- id = "params-json"
158- value = { paramsJson }
220+ { paramFields . length > 0 && (
221+ < FormFieldDefinition
222+ fields = { paramFields }
223+ values = { JSON . parse ( paramsJson ) }
159224 onChange = { handleParamsChange }
160- className = "form-control"
161- rows = "5"
162- placeholder = '{"key": "value"}'
225+ idPrefix = "param_"
163226 />
164- < p className = "text-xs text-gray-400 mt-1" > Enter JSON parameters for the action</ p >
165- </ div >
227+ ) }
166228
167229 < div className = "flex justify-end" >
168230 < button
0 commit comments