Skip to content

Commit

Permalink
Merge pull request #27
Browse files Browse the repository at this point in the history
* Changed Parameters from a json object to a typed dto's,
  • Loading branch information
ceesjansenflock authored Nov 29, 2024
1 parent 71e9956 commit ee47816
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@ 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
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
Expand All @@ -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(
Expand All @@ -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() },
)
},
),
Expand All @@ -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(),
Expand Down
70 changes: 69 additions & 1 deletion src/platform/wirespec/gateway.ws
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,81 @@ type TaskDto {
type ToolDto {
name: String,
description: String?,
parameters: String
parameters: ParameterDto[]
}

enum SenderDto {
Agent,
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?
Expand All @@ -52,6 +119,7 @@ type FatalResultDto {

type ResultDto = FinishedResultDto | StuckResultDto | FatalResultDto


enum MimeTypeDto {
IMAGE_JPEG,
IMAGE_PNG,
Expand Down

0 comments on commit ee47816

Please sign in to comment.