ztcl::agent check
Gives a value when it matches a result schema.
ztcl::agent check SCHEMA VALUE
Description
Gives VALUE when it matches the result schema SCHEMA, else the error ZTCL AGENT RESULT with the path of the first value that does not match.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| SCHEMA | result schema | required | A list of keys and types. |
| VALUE | dict | required | The value to check. |
Returns
VALUE.
Examples
Each example of this page runs after
package require ztcl::agent
# A fake model on 127.0.0.1, on a port of the system, in a thread: each
# request gets the next reply of ::replies, and ::requests keeps them.
set srv [thread::create {
proc serve {c a p} {
fconfigure $c -translation binary
set len 0
set hs {}
while {[gets $c line] >= 0 && [set line [string trim $line]] ne ""} {
lappend hs $line
regexp -nocase {^content-length: *(\d+)} $line -> len
}
lappend ::requests [list $hs [read $c $len]]
set body [lindex $::replies 0]
set ::replies [lrange $::replies 1 end]
puts -nonewline $c "HTTP/1.1 $::status OK\r\nContent-Length: [string length $body]\r\nConnection: close\r\n\r\n$body"
close $c
}
set ::status 200
set ::replies {}
set ::requests {}
set ::port [lindex [fconfigure [socket -server serve -myaddr 127.0.0.1 0] -sockname] 2]
thread::wait
}]
set url http://127.0.0.1:[thread::send $srv {set ::port}]/v1/messages
# Sets the replies of the fake model, and gives the requests that it got.
proc replies {args} { thread::send $::srv [list set ::replies $args] }
proc requests {} { thread::send $::srv {set ::requests} }
# A reply of the Messages API with one call of a tool.
proc call {name input} { return "{\"content\":\[{\"type\":\"tool_use\",\"id\":\"t[incr ::id]\",\"name\":\"$name\",\"input\":$input}\],\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}" }
proc add {a b} { expr {$a + $b} }
.
% ztcl::agent check {n int tags {list string}} {n 3 tags {a b}}
n 3 tags {a b}
% ztcl::agent check {d {dict {a bool}} x number} {d {a yes} x 1.5}
d {a yes} x 1.5
Errors
% ztcl::agent check {kind {enum a b}} {kind c}
result.kind is not enum a b
-errorcode ZTCL AGENT RESULT
% ztcl::agent check {n int} {n x}
result.n is not int
-errorcode ZTCL AGENT RESULT
% ztcl::agent check {n int} {n 1 m 2}
result has the extra key "m"
-errorcode ZTCL AGENT RESULT
% ztcl::agent check {n int} {}
result has no key "n"
-errorcode ZTCL AGENT RESULT
% ztcl::agent check {n bogus} {n 1}
bad type "bogus"
-errorcode ZTCL AGENT SCHEMA
% ztcl::agent check {n int}
wrong # args: should be "ztcl::agent check spec value"
-errorcode TCL WRONGARGS
Facts
- Capability
thread. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1