@@ -20,7 +20,7 @@ use golem_common::model::agent::bindings::golem::agent::common::{
2020 AgentError , DataValue , RegisteredAgentType ,
2121} ;
2222use golem_common:: model:: agent:: wit_naming:: ToWitNaming ;
23- use golem_common:: model:: agent:: { AgentId , AgentTypeName , ConfigKeyValueType , ConfigValueType } ;
23+ use golem_common:: model:: agent:: { AgentId , AgentTypeName , ConfigValueType } ;
2424use golem_common:: model:: oplog:: host_functions:: {
2525 GolemAgentCreateWebhook , GolemAgentGetAgentType , GolemAgentGetAllAgentTypes ,
2626} ;
@@ -31,7 +31,7 @@ use golem_common::model::oplog::{
3131} ;
3232use golem_common:: model:: PromiseId ;
3333use golem_wasm:: analysis:: AnalysedType ;
34- use golem_wasm:: { NodeBuilder , WitValue , WitValueBuilderExtensions } ;
34+ use golem_wasm:: { NodeBuilder , WitType , WitValue , WitValueBuilderExtensions } ;
3535
3636impl < Ctx : WorkerCtx > Host for DurableWorkerCtx < Ctx > {
3737 async fn get_all_agent_types ( & mut self ) -> anyhow:: Result < Vec < RegisteredAgentType > > {
@@ -220,42 +220,69 @@ impl<Ctx: WorkerCtx> Host for DurableWorkerCtx<Ctx> {
220220 }
221221 }
222222
223- async fn get_config_value ( & mut self , key : Vec < String > ) -> anyhow:: Result < WitValue > {
224- tracing:: debug!( "Agent getting config value for key {}" , key. join( "." ) ) ;
223+ async fn get_config_value (
224+ & mut self ,
225+ key : Vec < String > ,
226+ expected_type : WitType ,
227+ ) -> anyhow:: Result < WitValue > {
228+ let key_str = key. join ( "." ) ;
229+ tracing:: debug!( "Agent getting config value for key {}" , key_str) ;
225230
226231 let agent_id = self
227232 . agent_id ( )
228233 . ok_or_else ( || anyhow ! ( "only agentic workers can access agent config" ) ) ?;
229234
235+ let expected_type = AnalysedType :: from ( expected_type) ;
236+
230237 let declaration = self
231238 . component_metadata ( )
232239 . metadata
233240 . agent_types ( )
234241 . iter ( )
235242 . find ( |at| at. type_name == agent_id. agent_type )
236- . unwrap ( )
243+ . expect ( "Active agent type of agent was not declared in component metadata" )
237244 . config
238245 . iter ( )
239- . find ( |c| c. key == key) ;
246+ . find_map ( |c| ( c. key == key) . then ( || c. value . clone ( ) ) ) ;
247+
248+ let declaration = match declaration {
249+ None if matches ! ( expected_type, AnalysedType :: Option ( _) ) => {
250+ // Allow optional undeclared config for schema evolution
251+ return Ok ( WitValue :: builder ( ) . option_none ( ) ) ;
252+ }
253+ None => {
254+ return Err ( anyhow ! ( "No config declared for key {}" , key_str) ) ;
255+ }
256+ Some ( d) => d,
257+ } ;
240258
241259 match declaration {
242- Some ( ConfigKeyValueType {
243- value : ConfigValueType :: Local ( config_declaration) ,
244- ..
245- } ) => match self . state . local_agent_config . get ( & key) {
246- Some ( config_value) => Ok ( config_value. value . clone ( ) . into ( ) ) ,
247- None if matches ! ( config_declaration. value, AnalysedType :: Option ( _) ) => {
248- Ok ( WitValue :: builder ( ) . option_none ( ) )
260+ ConfigValueType :: Local ( local_decl) => {
261+ let config_value = self . state . local_agent_config . get ( & key) ;
262+
263+ match ( & local_decl. value , & expected_type, config_value) {
264+ // Declared optional, expected optional, value missing
265+ ( AnalysedType :: Option ( declared) , AnalysedType :: Option ( expected) , None )
266+ if declared == expected =>
267+ {
268+ Ok ( WitValue :: builder ( ) . option_none ( ) )
269+ }
270+
271+ // Types match and value exists
272+ ( declared, expected, Some ( value) ) if declared == expected => {
273+ Ok ( value. value . clone ( ) . into ( ) )
274+ }
275+
276+ _ => Err ( anyhow ! (
277+ "declared and expected type for config key {} are not compatible" ,
278+ key_str
279+ ) ) ,
249280 }
250- None => Err ( anyhow ! ( "No config declared for key {}" , key. join( "." ) ) ) ,
251- } ,
252- Some ( ConfigKeyValueType {
253- value : ConfigValueType :: Shared ( _) ,
254- ..
255- } ) => {
281+ }
282+
283+ ConfigValueType :: Shared ( _) => {
256284 unimplemented ! ( )
257285 }
258- None => Err ( anyhow ! ( "No config declared for key {}" , key. join( "." ) ) ) ,
259286 }
260287 }
261288}
0 commit comments