@@ -3,6 +3,7 @@ package ai.ancf.lmos.wot.integration
33
44import ai.ancf.lmos.wot.JsonMapper
55import ai.ancf.lmos.wot.Wot
6+ import ai.ancf.lmos.wot.security.NoSecurityScheme
67import ai.ancf.lmos.wot.security.SecurityScheme
78import ai.ancf.lmos.wot.thing.schema.*
89import com.fasterxml.jackson.databind.JsonNode
@@ -25,17 +26,26 @@ object ThingToFunctionsMapper {
2526
2627 private val functionCache = mutableMapOf<String , List <LLMFunction >>()
2728
28- suspend fun exploreToolDirectory (wot : Wot , functions : Functions , url : String , securityScheme : SecurityScheme ): List <LLMFunction > {
29+ suspend fun exploreToolDirectory (wot : Wot , functions : Functions , group : String , url : String , securityScheme : SecurityScheme ): List <LLMFunction > {
2930 val thingDescriptions = wot.exploreDirectory(url, securityScheme)
3031 val consumedThings = consumeThings(wot, thingDescriptions)
31- val retrieveAllFunction = createRetrieveAllFunction(functions, thingDescriptions)
32- mapAllThingFunctions(functions, consumedThings)
32+ val retrieveAllFunction = createRetrieveAllFunction(functions, group, thingDescriptions)
33+ mapAllThingFunctions(functions, group, consumedThings)
3334 val allFunctions = retrieveAllFunction+ functionCache.values.flatten()
3435 return allFunctions
3536 }
3637
37- private fun createRetrieveAllFunction (functions : Functions , thingDescriptions : Set <WoTThingDescription >): List <LLMFunction > {
38- return functions(" retrieveAllThings" , " Returns the metadata information of all available devices." , " all_things" ) {
38+ suspend fun requestThingDescription (wot : Wot , functions : Functions , group : String , url : String , securityScheme : SecurityScheme = NoSecurityScheme ()): List <LLMFunction > {
39+ val thingDescription = wot.requestThingDescription(url, securityScheme)
40+ val consumedThings = consumeThings(wot, setOf (thingDescription))
41+ val retrieveAllFunction = createRetrieveAllFunction(functions, group, setOf (thingDescription))
42+ mapAllThingFunctions(functions, group, consumedThings)
43+ val allFunctions = retrieveAllFunction+ functionCache.values.flatten()
44+ return allFunctions
45+ }
46+
47+ private fun createRetrieveAllFunction (functions : Functions , group : String , thingDescriptions : Set <WoTThingDescription >): List <LLMFunction > {
48+ return functions(" retrieveAllThings" , " Returns the metadata information of all available devices." , group) {
3949 summarizeThingDescriptions(thingDescriptions)
4050 }
4151 }
@@ -46,8 +56,8 @@ object ThingToFunctionsMapper {
4656 }
4757 }
4858
49- private fun mapAllThingFunctions (functions : Functions , consumedThings : List <WoTConsumedThing >): List <LLMFunction > {
50- return consumedThings.flatMap { mapThingDescriptionToFunctions(functions, it) }
59+ private fun mapAllThingFunctions (functions : Functions , group : String , consumedThings : List <WoTConsumedThing >): List <LLMFunction > {
60+ return consumedThings.flatMap { mapThingDescriptionToFunctions(functions, group, it) }
5161 }
5262
5363 private fun summarizeThingDescriptions (things : Set <WoTThingDescription >): String {
@@ -71,25 +81,25 @@ object ThingToFunctionsMapper {
7181 return thing.properties.entries.joinToString(" \n " ) { (key, property) -> " $key : ${property.title} - ${property.description} " }
7282 }
7383
74- private fun mapThingDescriptionToFunctions (functions : Functions , thing : WoTConsumedThing ): Set <LLMFunction > {
84+ private fun mapThingDescriptionToFunctions (functions : Functions , group : String , thing : WoTConsumedThing ): Set <LLMFunction > {
7585 val thingDescription = thing.getThingDescription()
7686 val defaultParams = createDefaultParams()
77- val actionFunctions = createActionFunctions(functions, thingDescription, defaultParams)
78- val propertyFunctions = createPropertyFunctions(functions, thingDescription, defaultParams)
79- val readAllPropertiesFunction = createReadAllPropertiesFunction(functions, thingDescription)
87+ val actionFunctions = createActionFunctions(functions, group, thingDescription, defaultParams)
88+ val propertyFunctions = createPropertyFunctions(functions, group, thingDescription, defaultParams)
89+ val readAllPropertiesFunction = createReadAllPropertiesFunction(functions, group, thingDescription)
8090 return actionFunctions.toSet() + propertyFunctions.toSet() + readAllPropertiesFunction.toSet()
8191 }
8292
8393 private fun createDefaultParams (): List <Pair <ParameterSchema , Boolean >> {
8494 return listOf (Pair (ParameterSchema (" thingId" , " The unique identifier of the thing" , ParameterType (" string" ), emptyList()), true ))
8595 }
8696
87- private fun createActionFunctions (functions : Functions , thingDescription : WoTThingDescription , defaultParams : List <Pair <ParameterSchema , Boolean >>): List <LLMFunction > {
97+ private fun createActionFunctions (functions : Functions , group : String , thingDescription : WoTThingDescription , defaultParams : List <Pair <ParameterSchema , Boolean >>): List <LLMFunction > {
8898 return thingDescription.actions.flatMap { (actionName, action) ->
8999 val actionParams = action.input?.let { listOf (Pair (mapDataSchemaToParam(it), true )) } ? : emptyList()
90100 val params = defaultParams + actionParams
91101 functionCache.getOrPut(actionName) {
92- functions(actionName, action.description ? : " No Description available" , thingDescription.title , params) { (thingId, input) ->
102+ functions(actionName, action.description ? : " No Description available" , group , params) { (thingId, input) ->
93103 invokeAction(thingDescription.id, actionName, input, action.input)
94104 }
95105 }
@@ -107,36 +117,36 @@ object ThingToFunctionsMapper {
107117 }
108118 }
109119
110- private fun createReadAllPropertiesFunction (functions : Functions , thingDescription : WoTThingDescription ): List <LLMFunction > {
120+ private fun createReadAllPropertiesFunction (functions : Functions , group : String , thingDescription : WoTThingDescription ): List <LLMFunction > {
111121 val functionKey = " readAllProperties"
112122 return functionCache.getOrPut(functionKey) {
113- functions(functionKey, " Read all properties of a thing" , " This function retrieves all properties of a thing " ) {
123+ functions(functionKey, " Read all properties of a thing" , group ) {
114124 thingDescriptionsMap[thingDescription.id]?.readAllProperties()?.map { (propertyName, futureValue) -> " $propertyName : ${futureValue.value().asText()} " }?.joinToString(" \n " ) ? : " Function call failed"
115125 }
116126 }
117127 }
118128
119- private fun createPropertyFunctions (functions : Functions , thingDescription : WoTThingDescription , defaultParams : List <Pair <ParameterSchema , Boolean >>): List <LLMFunction > {
129+ private fun createPropertyFunctions (functions : Functions , group : String , thingDescription : WoTThingDescription , defaultParams : List <Pair <ParameterSchema , Boolean >>): List <LLMFunction > {
120130 return thingDescription.properties.flatMap { (propertyName, property) ->
121131 when {
122- property.readOnly -> createReadPropertyFunction(functions, thingDescription , propertyName)
123- property.writeOnly -> createWritePropertyFunction(functions, thingDescription , propertyName, property, defaultParams)
124- else -> createReadPropertyFunction(functions, thingDescription , propertyName) + createWritePropertyFunction(functions, thingDescription , propertyName, property, defaultParams)
132+ property.readOnly -> createReadPropertyFunction(functions, group , propertyName)
133+ property.writeOnly -> createWritePropertyFunction(functions, group , propertyName, property, defaultParams)
134+ else -> createReadPropertyFunction(functions, group , propertyName) + createWritePropertyFunction(functions, group , propertyName, property, defaultParams)
125135 }
126136 }
127137 }
128138
129139 private fun createReadPropertyFunction (
130140 functions : Functions ,
131- thingDescription : WoTThingDescription ,
141+ group : String ,
132142 propertyName : String
133143 ) : List <LLMFunction > {
134144 val functionKey = " read_$propertyName "
135145 return functionCache.getOrPut(functionKey) {
136146 functions(
137147 functionKey,
138148 " Reads the value of the $propertyName property." ,
139- thingDescription.title
149+ group,
140150 ) { (thingId) ->
141151 try {
142152 thingDescriptionsMap[thingId]?.readProperty(propertyName)?.value()?.asText() ? : " Function call failed"
@@ -150,7 +160,7 @@ object ThingToFunctionsMapper {
150160
151161 private fun createWritePropertyFunction (
152162 functions : Functions ,
153- thingDescription : WoTThingDescription ,
163+ group : String ,
154164 propertyName : String ,
155165 property : PropertyAffordance <* >,
156166 defaultParams : List <Pair <ParameterSchema , Boolean >>
@@ -161,7 +171,7 @@ object ThingToFunctionsMapper {
161171 functions(
162172 functionKey,
163173 " Sets the value of the $propertyName property." ,
164- thingDescription.title ,
174+ group ,
165175 params
166176 ) { (thingId, propertyValue) ->
167177 if (propertyValue != null ) {
0 commit comments