From ee478164096907ee30b33e1a25c9ccbdfba013a5 Mon Sep 17 00:00:00 2001 From: ceesjansenflock Date: Fri, 29 Nov 2024 14:07:34 +0100 Subject: [PATCH] Merge pull request #27 * Changed Parameters from a json object to a typed dto's, --- .../aigentic/platform/mapper/RequestMapper.kt | 103 ++++++++++++++++-- src/platform/wirespec/gateway.ws | 70 +++++++++++- 2 files changed, 160 insertions(+), 13 deletions(-) diff --git a/src/platform/src/commonMain/kotlin/community/flock/aigentic/platform/mapper/RequestMapper.kt b/src/platform/src/commonMain/kotlin/community/flock/aigentic/platform/mapper/RequestMapper.kt index 07c74d6..3310203 100644 --- a/src/platform/src/commonMain/kotlin/community/flock/aigentic/platform/mapper/RequestMapper.kt +++ b/src/platform/src/commonMain/kotlin/community/flock/aigentic/platform/mapper/RequestMapper.kt @@ -8,6 +8,10 @@ import community.flock.aigentic.core.message.Message import community.flock.aigentic.core.message.MimeType import community.flock.aigentic.core.message.Sender import community.flock.aigentic.core.message.ToolCall +import community.flock.aigentic.core.tool.Parameter +import community.flock.aigentic.core.tool.ParameterType +import community.flock.aigentic.core.tool.ParameterType.Primitive +import community.flock.aigentic.core.tool.PrimitiveValue import community.flock.aigentic.gateway.wirespec.Base64MessageDto import community.flock.aigentic.gateway.wirespec.ConfigDto import community.flock.aigentic.gateway.wirespec.FatalResultDto @@ -15,6 +19,18 @@ import community.flock.aigentic.gateway.wirespec.FinishedResultDto import community.flock.aigentic.gateway.wirespec.MessageDto import community.flock.aigentic.gateway.wirespec.MimeTypeDto import community.flock.aigentic.gateway.wirespec.ModelRequestInfoDto +import community.flock.aigentic.gateway.wirespec.ParameterArrayDto +import community.flock.aigentic.gateway.wirespec.ParameterDto +import community.flock.aigentic.gateway.wirespec.ParameterEnumDto +import community.flock.aigentic.gateway.wirespec.ParameterObjectDto +import community.flock.aigentic.gateway.wirespec.ParameterPrimitiveDto +import community.flock.aigentic.gateway.wirespec.ParameterTypeDto +import community.flock.aigentic.gateway.wirespec.PrimitiveValueBooleanDto +import community.flock.aigentic.gateway.wirespec.PrimitiveValueDto +import community.flock.aigentic.gateway.wirespec.PrimitiveValueIntegerDto +import community.flock.aigentic.gateway.wirespec.PrimitiveValueNumberDto +import community.flock.aigentic.gateway.wirespec.PrimitiveValueStringDto +import community.flock.aigentic.gateway.wirespec.PrimitiveValueTypeDto import community.flock.aigentic.gateway.wirespec.RunDto import community.flock.aigentic.gateway.wirespec.SenderDto import community.flock.aigentic.gateway.wirespec.StuckResultDto @@ -26,11 +42,6 @@ import community.flock.aigentic.gateway.wirespec.ToolCallsMessageDto import community.flock.aigentic.gateway.wirespec.ToolDto import community.flock.aigentic.gateway.wirespec.ToolResultMessageDto import community.flock.aigentic.gateway.wirespec.UrlMessageDto -import community.flock.aigentic.providers.jsonschema.emitPropertiesAndRequired -import kotlinx.serialization.encodeToString -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.buildJsonObject -import kotlinx.serialization.json.put fun Run.toDto(agent: Agent) = RunDto( @@ -50,13 +61,7 @@ fun Run.toDto(agent: Agent) = ToolDto( name = name.value, description = tool.description, - parameters = - Json.encodeToString( - buildJsonObject { - put("type", "object") - emitPropertiesAndRequired(tool.parameters) - }, - ), + parameters = tool.parameters.map { it.toDto() }, ) }, ), @@ -65,6 +70,80 @@ fun Run.toDto(agent: Agent) = result = result.toDto(), ) +private fun Parameter.toDto(): ParameterDto = + when (this) { + is Parameter.Primitive -> + ParameterPrimitiveDto( + name = name, + description = description, + isRequired = isRequired, + paramType = type.toDto(), + ) + + is Parameter.Complex.Object -> + ParameterObjectDto( + name = name, + description = description, + isRequired = isRequired, + paramType = type.toDto(), + parameters = parameters.map { it.toDto() }, + ) + + is Parameter.Complex.Array -> + ParameterArrayDto( + name = name, + description = description, + isRequired = isRequired, + paramType = type.toDto(), + itemDefinition = itemDefinition.toDto(), + ) + + is Parameter.Complex.Enum -> + + ParameterEnumDto( + name = name, + description = description, + isRequired = isRequired, + paramType = type.toDto(), + values = + values.map { value -> + val parameterType = determineType(value) + this.createEnumValues(parameterType, value) + }, + valueType = valueType.toDto(), + ) + } + +private fun determineType(value: PrimitiveValue<*>): PrimitiveValueTypeDto = + when (value) { + is PrimitiveValue.String -> PrimitiveValueTypeDto.STRING + is PrimitiveValue.Number -> PrimitiveValueTypeDto.NUMBER + is PrimitiveValue.Boolean -> PrimitiveValueTypeDto.BOOLEAN + is PrimitiveValue.Integer -> PrimitiveValueTypeDto.INTEGER + } + +private fun Parameter.Complex.Enum.createEnumValues( + parameterType: PrimitiveValueTypeDto, + value: PrimitiveValue<*>, +): PrimitiveValueDto = + when (parameterType) { + PrimitiveValueTypeDto.STRING -> PrimitiveValueStringDto(value.toString()) + PrimitiveValueTypeDto.INTEGER -> PrimitiveValueIntegerDto(value.toString().toLong()) + PrimitiveValueTypeDto.BOOLEAN -> PrimitiveValueBooleanDto(value.toString().toBoolean()) + PrimitiveValueTypeDto.NUMBER -> PrimitiveValueNumberDto(value.toString().toDouble()) + } + +private fun ParameterType.toDto() = + when (this) { + is Primitive.String -> ParameterTypeDto.STRING + is Primitive.Boolean -> ParameterTypeDto.BOOLEAN + is Primitive.Number -> ParameterTypeDto.NUMBER + is Primitive.Integer -> ParameterTypeDto.INTEGER + is ParameterType.Complex.Object -> ParameterTypeDto.OBJECT + is ParameterType.Complex.Array -> ParameterTypeDto.ARRAY + is ParameterType.Complex.Enum -> ParameterTypeDto.ENUM + } + private fun ModelRequestInfo.toDto(): ModelRequestInfoDto = ModelRequestInfoDto( startedAt = startedAt.toString(), diff --git a/src/platform/wirespec/gateway.ws b/src/platform/wirespec/gateway.ws index 75606c8..8845ecc 100644 --- a/src/platform/wirespec/gateway.ws +++ b/src/platform/wirespec/gateway.ws @@ -29,7 +29,7 @@ type TaskDto { type ToolDto { name: String, description: String?, - parameters: String + parameters: ParameterDto[] } enum SenderDto { @@ -37,6 +37,73 @@ enum SenderDto { Model } + +enum ParameterTypeDto { + STRING, + NUMBER, + INTEGER, + BOOLEAN, + ARRAY, + ENUM, + OBJECT +} +type ParameterDto = ParameterPrimitiveDto | ParameterObjectDto | ParameterArrayDto |ParameterEnumDto + +type ParameterPrimitiveDto { + name: String, + description: String?, + isRequired: Boolean, + paramType: ParameterTypeDto +} + +type ParameterObjectDto { + name: String, + description: String?, + isRequired: Boolean, + paramType: ParameterTypeDto, + parameters: ParameterDto[] + +} + +type ParameterArrayDto { + name: String, + description: String?, + isRequired: Boolean, + paramType: ParameterTypeDto, + itemDefinition: ParameterDto +} + +type ParameterEnumDto { + name: String, + description: String?, + isRequired: Boolean, + paramType: ParameterTypeDto, + default: PrimitiveValueDto?, + values: PrimitiveValueDto[], + valueType: ParameterTypeDto +} +type PrimitiveValueDto = PrimitiveValueStringDto | PrimitiveValueNumberDto | PrimitiveValueIntegerDto | PrimitiveValueBooleanDto +type PrimitiveValueStringDto { + value: String +} + +type PrimitiveValueNumberDto { + value: Number +} + +type PrimitiveValueIntegerDto { + value: Integer +} +type PrimitiveValueBooleanDto { + value: Boolean +} +enum PrimitiveValueTypeDto { + STRING, + NUMBER, + INTEGER, + BOOLEAN +} + type FinishedResultDto { description:String, response: String? @@ -52,6 +119,7 @@ type FatalResultDto { type ResultDto = FinishedResultDto | StuckResultDto | FatalResultDto + enum MimeTypeDto { IMAGE_JPEG, IMAGE_PNG,