1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
| import upickle.default._
import ujson._
/**
* Safe parameter extraction
*/
case class SafeParameterExtractor(params: ujson.Value) {
def getString(path: String): Either[String, String] =
extract(path, _.strOpt, "string")
def getInt(path: String): Either[String, Int] =
extract(path, _.numOpt.map(_.toInt), "integer")
def getDouble(path: String): Either[String, Double] =
extract(path, _.numOpt, "number")
def getBoolean(path: String): Either[String, Boolean] =
extract(path, _.boolOpt, "boolean")
def getArray(path: String): Either[String, ujson.Arr] =
extract(path, v => Option(v).collect { case arr: ujson.Arr => arr }, "array")
def getObject(path: String): Either[String, ujson.Obj] =
extract(path, v => Option(v).collect { case obj: ujson.Obj => obj }, "object")
// Generic extractor with type validation
private def extract[T](path: String, extractor: ujson.Value => Option[T], expectedType: String): Either[String, T] = {
try {
val pathParts = path.split('.')
var current = params
// Navigate through nested path
for (part <- pathParts.dropRight(1)) {
current.obj.get(part) match {
case Some(value) => current = value
case None => return Left(s"Path '$path' not found: missing '$part' segment")
}
}
// Extract the final value
val finalPart = pathParts.last
current.obj.get(finalPart) match {
case Some(value) =>
extractor(value) match {
case Some(result) => Right(result)
case None => Left(s"Value at '$path' is not of expected type '$expectedType'")
}
case None => Left(s"Parameter '$finalPart' not found")
}
} catch {
case e: Exception => Left(s"Error extracting parameter at '$path': ${e.getMessage}")
}
}
}
/**
* Core model for tool function definitions
*/
case class ToolFunction[T, R: ReadWriter](
name: String,
description: String,
schema: SchemaDefinition[T],
handler: SafeParameterExtractor => Either[String, R]
) {
/**
* Converts the tool definition to the format expected by OpenAI's API
*/
def toOpenAITool(strict: Boolean = true): ujson.Value = {
ujson.Obj(
"type" -> ujson.Str("function"),
"function" -> ujson.Obj(
"name" -> ujson.Str(name),
"description" -> ujson.Str(description),
"parameters" -> schema.toJsonSchema,
"strict" -> ujson.Bool(strict)
)
)
}
/**
* Executes the tool with the given arguments
*/
def execute(args: ujson.Value): Either[ToolCallError, R] = {
val extractor = SafeParameterExtractor(args)
handler(extractor) match {
case Right(result) => Right(result)
case Left(error) => Left(ToolCallError.InvalidArguments(List(error)))
}
}
}
/**
* Schema definitions
*/
sealed trait SchemaDefinition[T] {
def toJsonSchema: ujson.Value
}
/**
* Schema building blocks
*/
case class StringSchema(
description: String,
enumValues: Option[Seq[String]] = None,
pattern: Option[String] = None,
minLength: Option[Int] = None,
maxLength: Option[Int] = None
) extends SchemaDefinition[String] {
def toJsonSchema: ujson.Value = {
val base = ujson.Obj(
"type" -> ujson.Str("string"),
"description" -> ujson.Str(description)
)
enumValues.foreach(values => base("enum") = ujson.Arr.from(values.map(ujson.Str(_))))
pattern.foreach(p => base("pattern") = ujson.Str(p))
minLength.foreach(min => base("minLength") = ujson.Num(min))
maxLength.foreach(max => base("maxLength") = ujson.Num(max))
base
}
def withEnum(values: Seq[String]): StringSchema = copy(enumValues = Some(values))
def withPattern(regex: String): StringSchema = copy(pattern = Some(regex))
def withLengthConstraints(min: Option[Int] = None, max: Option[Int] = None): StringSchema =
copy(minLength = min, maxLength = max)
}
case class NumberSchema(
description: String,
isInteger: Boolean = false,
minimum: Option[Double] = None,
maximum: Option[Double] = None,
exclusiveMinimum: Option[Double] = None,
exclusiveMaximum: Option[Double] = None,
multipleOf: Option[Double] = None
) extends SchemaDefinition[Double] {
def toJsonSchema: ujson.Value = {
val base = ujson.Obj(
"type" -> ujson.Str(if (isInteger) "integer" else "number"),
"description" -> ujson.Str(description)
)
minimum.foreach(min => base("minimum") = ujson.Num(min))
maximum.foreach(max => base("maximum") = ujson.Num(max))
exclusiveMinimum.foreach(min => base("exclusiveMinimum") = ujson.Num(min))
exclusiveMaximum.foreach(max => base("exclusiveMaximum") = ujson.Num(max))
multipleOf.foreach(multiple => base("multipleOf") = ujson.Num(multiple))
base
}
def withRange(min: Option[Double] = None, max: Option[Double] = None): NumberSchema =
copy(minimum = min, maximum = max)
def withExclusiveRange(min: Option[Double] = None, max: Option[Double] = None): NumberSchema =
copy(exclusiveMinimum = min, exclusiveMaximum = max)
def withMultipleOf(multiple: Double): NumberSchema =
copy(multipleOf = Some(multiple))
}
case class IntegerSchema(
description: String,
minimum: Option[Int] = None,
maximum: Option[Int] = None,
exclusiveMinimum: Option[Int] = None,
exclusiveMaximum: Option[Int] = None,
multipleOf: Option[Int] = None
) extends SchemaDefinition[Int] {
def toJsonSchema: ujson.Value = {
val base = ujson.Obj(
"type" -> ujson.Str("integer"),
"description" -> ujson.Str(description)
)
minimum.foreach(min => base("minimum") = ujson.Num(min))
maximum.foreach(max => base("maximum") = ujson.Num(max))
exclusiveMinimum.foreach(min => base("exclusiveMinimum") = ujson.Num(min))
exclusiveMaximum.foreach(max => base("exclusiveMaximum") = ujson.Num(max))
multipleOf.foreach(multiple => base("multipleOf") = ujson.Num(multiple))
base
}
def withRange(min: Option[Int] = None, max: Option[Int] = None): IntegerSchema =
copy(minimum = min, maximum = max)
def withExclusiveRange(min: Option[Int] = None, max: Option[Int] = None): IntegerSchema =
copy(exclusiveMinimum = min, exclusiveMaximum = max)
def withMultipleOf(multiple: Int): IntegerSchema =
copy(multipleOf = Some(multiple))
}
case class BooleanSchema(
description: String
) extends SchemaDefinition[Boolean] {
def toJsonSchema: ujson.Value = {
ujson.Obj(
"type" -> ujson.Str("boolean"),
"description" -> ujson.Str(description)
)
}
}
case class ArraySchema[A](
description: String,
itemSchema: SchemaDefinition[A],
minItems: Option[Int] = None,
maxItems: Option[Int] = None,
uniqueItems: Boolean = false
) extends SchemaDefinition[Seq[A]] {
def toJsonSchema: ujson.Value = {
val base = ujson.Obj(
"type" -> ujson.Str("array"),
"description" -> ujson.Str(description),
"items" -> itemSchema.toJsonSchema
)
minItems.foreach(min => base("minItems") = ujson.Num(min))
maxItems.foreach(max => base("maxItems") = ujson.Num(max))
if (uniqueItems) base("uniqueItems") = ujson.Bool(true)
base
}
def withSizeConstraints(min: Option[Int] = None, max: Option[Int] = None): ArraySchema[A] =
copy(minItems = min, maxItems = max)
def withUniqueItems(unique: Boolean = true): ArraySchema[A] =
copy(uniqueItems = unique)
}
case class PropertyDefinition[T](
name: String,
schema: SchemaDefinition[T],
required: Boolean = true
)
case class ObjectSchema[T](
description: String,
properties: Seq[PropertyDefinition[_]],
additionalProperties: Boolean = false
) extends SchemaDefinition[T] {
def toJsonSchema: ujson.Value = {
val props = ujson.Obj()
val required = properties.filter(_.required).map(_.name)
properties.foreach { prop =>
props(prop.name) = prop.schema.toJsonSchema
}
ujson.Obj(
"type" -> ujson.Str("object"),
"description" -> ujson.Str(description),
"properties" -> props,
"required" -> ujson.Arr.from(required.map(ujson.Str(_))),
"additionalProperties" -> ujson.Bool(additionalProperties)
)
}
def withProperty[P](property: PropertyDefinition[P]): ObjectSchema[T] = {
copy(properties = properties :+ property)
}
}
case class NullableSchema[T](
underlying: SchemaDefinition[T]
) extends SchemaDefinition[Option[T]] {
def toJsonSchema: ujson.Value = {
val schema = underlying.toJsonSchema.obj
val typeField = schema.get("type")
typeField match {
case Some(ujson.Str(typeValue)) =>
// Replace type field with array of types
schema("type") = ujson.Arr(ujson.Str(typeValue), ujson.Str("null"))
case Some(arr: ujson.Arr) =>
// Add null to existing type array
schema("type") = arr.value :+ ujson.Str("null")
case _ =>
// Create new type array if none exists
schema("type") = ujson.Arr(ujson.Str("null"))
}
ujson.Obj.from(schema)
}
}
/**
* Schema builder - fluent API
*/
object Schema {
// String schemas
def string(description: String): StringSchema = StringSchema(description)
// Number schemas
def number(description: String): NumberSchema = NumberSchema(description)
def integer(description: String): IntegerSchema = IntegerSchema(description)
// Boolean schemas
def boolean(description: String): BooleanSchema = BooleanSchema(description)
// Array schemas
def array[A](description: String, itemSchema: SchemaDefinition[A]): ArraySchema[A] =
ArraySchema(description, itemSchema)
// Object schemas
def `object`[T](description: String): ObjectSchema[T] =
ObjectSchema[T](description, Seq.empty)
// Nullable schemas
def nullable[T](schema: SchemaDefinition[T]): NullableSchema[T] =
NullableSchema(schema)
// Properties
def property[T](name: String, schema: SchemaDefinition[T], required: Boolean = true): PropertyDefinition[T] =
PropertyDefinition(name, schema, required)
}
/**
* Builder for tool definitions
*/
class ToolBuilder[T, R: ReadWriter] private (
name: String,
description: String,
schema: SchemaDefinition[T],
handler: Option[SafeParameterExtractor => Either[String, R]] = None
) {
def withHandler(handler: SafeParameterExtractor => Either[String, R]): ToolBuilder[T, R] =
new ToolBuilder(name, description, schema, Some(handler))
def build(): ToolFunction[T, R] = handler match {
case Some(h) => ToolFunction(name, description, schema, h)
case None => throw new IllegalStateException("Handler not defined")
}
}
object ToolBuilder {
def apply[T, R: ReadWriter](name: String, description: String, schema: SchemaDefinition[T]): ToolBuilder[T, R] =
new ToolBuilder(name, description, schema)
}
/**
* Request/Response handling
*/
case class ToolCallRequest(
functionName: String,
arguments: ujson.Value
)
sealed trait ToolCallError
object ToolCallError {
case class UnknownFunction(name: String) extends ToolCallError
case class InvalidArguments(errors: List[String]) extends ToolCallError
case class ExecutionError(cause: Throwable) extends ToolCallError
}
/**
* Tool registry and executor
*/
class ToolRegistry(tools: Seq[ToolFunction[_, _]]) {
// Get a specific tool by name
def getTool(name: String): Option[ToolFunction[_, _]] = tools.find(_.name == name)
// Execute a tool call
def execute(request: ToolCallRequest): Either[ToolCallError, ujson.Value] = {
tools.find(_.name == request.functionName) match {
case Some(tool) =>
try {
tool.execute(request.arguments) match {
case Right(result) => Right(writeJs(result))
case Left(error) => Left(error)
}
} catch {
case e: Exception => Left(ToolCallError.ExecutionError(e))
}
case None => Left(ToolCallError.UnknownFunction(request.functionName))
}
}
// Generate OpenAI tool definitions for all tools
def getOpenAITools(strict: Boolean = true): ujson.Arr = {
ujson.Arr.from(tools.map(_.toOpenAITool(strict)))
}
// Generate a specific format of tool definitions for a particular LLM provider
def getToolDefinitions(provider: String): ujson.Value = provider.toLowerCase match {
case "openai" => getOpenAITools()
case "anthropic" => getOpenAITools() // Currently using the same format
case "gemini" => getOpenAITools() // May need adjustment for Google's format
case _ => throw new IllegalArgumentException(s"Unsupported LLM provider: $provider")
}
}
|