Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 62 additions & 4 deletions src/tools/registry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,19 +1347,77 @@ Each element is a cons of the declared name and its subschema."
(collect schema)))
(nreverse properties)))

(-> tool--corrected-value (json-object t) t)
(-> tool--integer-schema-p (t) boolean)
(defun tool--integer-schema-p (schema)
"Return true when SCHEMA declares the JSON integer type alone."
(and (json-object-p schema)
(equal (json-get schema "type") "integer")
t))

(-> tool--decimal-integer-text-p (string) boolean)
(defun tool--decimal-integer-text-p (text)
"Return true when TEXT is one plain decimal integer and nothing else."
(let ((start (if (and (plusp (length text))
(member (char text 0) '(#\+ #\-) :test #'char=))
1
0)))
(and (< start (length text))
(loop for index from start below (length text)
always (digit-char-p (char text index)))
t)))

(-> tool--boolean-schema-p (t) boolean)
(defun tool--boolean-schema-p (schema)
"Return true when SCHEMA declares the JSON boolean type alone."
(and (json-object-p schema)
(equal (json-get schema "type") "boolean")
t))

(-> tool--declared-boolean (string) t)
(defun tool--declared-boolean (text)
"Return TEXT as a JSON boolean when it spells one, else TEXT unchanged."
(cond
((string= text "true")
t)
((string= text "false")
(json-false))
(t
text)))

(-> tool--declared-scalar (t t) t)
(defun tool--declared-scalar (schema value)
"Return VALUE in the JSON type SCHEMA declares when its text form is exact.

A transport can carry a declared integer as decimal text or a declared boolean
as its JSON spelling. Accept those spellings so the call executes, and leave
every other value unchanged so a malformed value still reaches the argument
diagnostic."
(cond
((and (stringp value)
(tool--integer-schema-p schema)
(tool--decimal-integer-text-p value))
(parse-integer value))
((and (stringp value) (tool--boolean-schema-p schema))
(tool--declared-boolean value))
(t
value)))

(-> tool--corrected-value (t t) t)
(defun tool--corrected-value (schema value)
"Return VALUE with underscore-spelled names corrected against SCHEMA."
"Return VALUE corrected against SCHEMA.

A declared name accepts its underscore spelling, a declared integer accepts its
own decimal text, and a declared boolean accepts its JSON spelling."
(cond
((and (json-object-p schema) (json-object-p value))
(tool--corrected-object (tool--schema-properties schema) value))
((and (json-object-p schema) (vectorp value))
((and (json-object-p schema) (vectorp value) (not (stringp value)))
(let ((items (json-get schema "items")))
(if (json-object-p items)
(map 'simple-vector (lambda (element) (tool--corrected-value items element)) value)
value)))
(t
value)))
(tool--declared-scalar schema value))))

(-> tool--corrected-object (list json-object) json-object)
(defun tool--corrected-object (properties object)
Expand Down
40 changes: 39 additions & 1 deletion tests/tool-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,9 @@
(received nil)
(schema (tool-object-schema
(json-object
"path" (tool-string-property "Path")
"path" (tool-string-property "Path")
"count" (tool-integer-property "Count")
"flag" (tool-boolean-property "Flag")
"options" (tool-object-schema
(json-object "deep-name" (tool-string-property "Deep name"))
'("deep-name"))
Expand Down Expand Up @@ -771,6 +773,42 @@
"nested object names are corrected")
(test-assert (equal (json-get (aref (json-get received "rows") 0) "row-id") "1")
"array item names are corrected"))
(let ((result (tool-registry-execute-call
registry
(json-object "namespace" "probe" "name" "contract"
"arguments" "{\"path\":\"workspace:probe\",\"count\":\"7\"}")
context)))
(test-assert (tool-result-success-p result)
"a declared integer accepts its decimal text")
(test-assert (eql (json-get received "count") 7)
"the decimal text becomes an integer argument"))
(let ((result (tool-registry-execute-call
registry
(json-object "namespace" "probe" "name" "contract"
"arguments" "{\"path\":\"workspace:probe\",\"count\":\"7.5\"}")
context)))
(test-assert (tool-result-success-p result)
"a non-integer spelling still reaches dispatch")
(test-assert (equal (json-get received "count") "7.5")
"only exact decimal text becomes an integer"))
(let ((result (tool-registry-execute-call
registry
(json-object "namespace" "probe" "name" "contract"
"arguments" "{\"path\":\"workspace:probe\",\"flag\":\"true\"}")
context)))
(test-assert (tool-result-success-p result)
"a declared boolean accepts its JSON spelling")
(test-assert (json-true-p (json-get received "flag"))
"the true spelling becomes a JSON boolean"))
(let ((result (tool-registry-execute-call
registry
(json-object "namespace" "probe" "name" "contract"
"arguments" "{\"path\":\"workspace:probe\",\"flag\":\"maybe\"}")
context)))
(test-assert (tool-result-success-p result)
"a non-boolean spelling still reaches dispatch")
(test-assert (equal (json-get received "flag") "maybe")
"only exact JSON spellings become booleans"))
(let ((result (tool-registry-execute-call
registry
(json-object "namespace" "probe" "name" "contract"
Expand Down
Loading