| | | 1 | | namespace UIBlazor.Agents; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Описание функции для нативного туллинга |
| | | 5 | | /// </summary> |
| | | 6 | | public class NativeToolDefinition |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Тип всегда функция |
| | | 10 | | /// </summary> |
| | | 11 | | [JsonPropertyName("type")] |
| | 147 | 12 | | public string Type { get; set; } = "function"; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Вот и сама функция |
| | | 16 | | /// </summary> |
| | | 17 | | [JsonPropertyName("function")] |
| | 335 | 18 | | public required NativeToolFunction Function { get; set; } |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Функция для нативного туллинга |
| | | 23 | | /// </summary> |
| | | 24 | | public class NativeToolFunction |
| | | 25 | | { |
| | | 26 | | [JsonPropertyName("name")] |
| | | 27 | | public required string Name { get; set; } |
| | | 28 | | |
| | | 29 | | [JsonPropertyName("description")] |
| | | 30 | | public required string Description { get; set; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Все поля обязательны или нет |
| | | 34 | | /// </summary> |
| | | 35 | | [JsonPropertyName("strict")] |
| | | 36 | | public bool Strict { get; set; } = true; |
| | | 37 | | |
| | | 38 | | [JsonPropertyName("parameters")] |
| | | 39 | | public required NativeParameters Parameters { get; set; } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Параметры функции для нативного туллинга |
| | | 44 | | /// </summary> |
| | | 45 | | public class NativeParameters |
| | | 46 | | { |
| | | 47 | | /// <summary> |
| | | 48 | | /// Тип параметра |
| | | 49 | | /// </summary> |
| | | 50 | | [JsonPropertyName("type")] |
| | | 51 | | public required string Type { get; set; } = NativeToolType.String; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Все свойства |
| | | 55 | | /// </summary> |
| | | 56 | | [JsonPropertyName("properties")] |
| | | 57 | | public required Dictionary<string, NativePropertyDefinition> Properties { get; set; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Массив с обязательными свойствами. При<see cref="NativeToolFunction.Strict"/> все должны быть обязательны |
| | | 61 | | /// </summary> |
| | | 62 | | [JsonPropertyName("required")] |
| | | 63 | | public List<string> Required { get; set; } = []; |
| | | 64 | | |
| | | 65 | | [JsonPropertyName("additionalProperties")] |
| | | 66 | | public bool AdditionalProperties { get; } = false; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Описание свойства функции для нативного туллинга |
| | | 71 | | /// </summary> |
| | | 72 | | public class NativePropertyDefinition |
| | | 73 | | { |
| | | 74 | | private string? _singleType; |
| | | 75 | | private string[]? _unionTypes; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Тип свойства. Может быть строкой или массивом строк для union типов. |
| | | 79 | | /// "string", "number", "integer", "boolean", "array", "object", "null" |
| | | 80 | | /// Для optional полей в Strict Mode: ["string", "null"] |
| | | 81 | | /// </summary> |
| | | 82 | | [JsonPropertyName("type")] |
| | | 83 | | public object Type |
| | | 84 | | { |
| | | 85 | | get => (object?)_unionTypes ?? _singleType ?? NativeToolType.Object; |
| | | 86 | | set |
| | | 87 | | { |
| | | 88 | | switch (value) |
| | | 89 | | { |
| | | 90 | | case string s: |
| | | 91 | | _singleType = s; |
| | | 92 | | _unionTypes = null; |
| | | 93 | | break; |
| | | 94 | | case string[] arr: |
| | | 95 | | _unionTypes = arr; |
| | | 96 | | _singleType = null; |
| | | 97 | | break; |
| | | 98 | | case JsonElement el when el.ValueKind == JsonValueKind.String: |
| | | 99 | | _singleType = el.GetString()!; |
| | | 100 | | _unionTypes = null; |
| | | 101 | | break; |
| | | 102 | | case JsonElement el when el.ValueKind == JsonValueKind.Array: |
| | | 103 | | _unionTypes = el.EnumerateArray().Select(e => e.GetString()!).ToArray(); |
| | | 104 | | _singleType = null; |
| | | 105 | | break; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Установить один тип |
| | | 112 | | /// </summary> |
| | | 113 | | public void SetSingleType(string type) |
| | | 114 | | { |
| | | 115 | | _singleType = type; |
| | | 116 | | _unionTypes = null; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Установить union типы для optional полей в Strict Mode |
| | | 121 | | /// </summary> |
| | | 122 | | public void SetUnionTypes(params string[] types) |
| | | 123 | | { |
| | | 124 | | _unionTypes = types; |
| | | 125 | | _singleType = null; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Проверить, является ли тип union (массив типов) |
| | | 130 | | /// </summary> |
| | | 131 | | [JsonIgnore] |
| | | 132 | | public bool IsUnionType => _unionTypes != null; |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Текст для LLM |
| | | 136 | | /// </summary> |
| | | 137 | | [JsonPropertyName("description")] |
| | | 138 | | [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| | | 139 | | public string? Description { get; set; } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Если Type = "array" |
| | | 143 | | /// </summary> |
| | | 144 | | [JsonPropertyName("items")] |
| | | 145 | | [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| | | 146 | | public object? Items { get; set; } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Если Type = "object" |
| | | 150 | | /// </summary> |
| | | 151 | | [JsonPropertyName("properties")] |
| | | 152 | | [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| | | 153 | | public Dictionary<string, NativePropertyDefinition>? Properties { get; set; } |
| | | 154 | | } |