nsmcp::CodexQuestionRun method receive (protected)

 <instance of nsmcp::CodexQuestionRun[i]> receive

Defined in /usr/local/ns/tcl/nsmcp/question-backend.tcl

Testcases:
No testcase defined.
Source code:
while {1} {
    :checkDeadline
    if {![:emit {type check}] && !${:cancelled}} {
        set :cancelled true
        if {${:turn} eq ""} {
            error "Question cancelled"
        }
        :send turn/interrupt [list threadId string ${:thread} turnId string ${:turn}]
        set :deadline [expr {min(${:deadline},[clock milliseconds]+10000)}]
    }
    set n [gets ${:channel} line]
    if {$n >= 0} {
        break
    }
    if {[eof ${:channel}]} {
        error "Codex App Server closed its connection"
    }
    if {[string bytelength $line] > 8388608} {
        error "Question backend event is too large"
    }
    ns_sleep 0.05
}
if {[string bytelength $line] > 8388608} {
    error "Question backend event is too large"
}
set :lastEvent response
set message [ns_json parse -maxdepth 64 -maxstring 8388608 -maxcontainer 100000 -- $line]
if {![dict exists $message method]} {
    if {[dict exists $message id]} {
        dict set :replies [dict get $message id] $message
    }
    return
}
set method [dict get $message method]
# Notification names are untrusted input; bound and validate log metadata.
if {[regexp {^[A-Za-z][A-Za-z0-9/_-]{0,95}$} $method]} {
    set :lastEvent $method
} else {
    set :lastEvent notification
}
if {[dict exists $message id]} {
    set raw [ns_json parse -output triples -- $line]
    set idType [ns_json triples gettype -path id -- $raw]
    puts ${:writeChannel} [ns_json value -- [list {} object [list id $idType [dict get $message id]  error object {code number -32601 message string {Interactive requests are unavailable in this diagnostics interface}}]]]
    flush ${:writeChannel}
    return
}
set params [dict get $message params]
switch -- $method {
    item/agentMessage/delta {
        :emit [dict create type text text [dict get $params delta]]
    }
    item/started {
        :observeItem started [dict get $params item]
        if {[dict exists $params item type] && [dict get $params item type] eq "mcpToolCall"} {
            :toolStart [dict get $params item]
        }
    }
    item/completed {
        set item [dict get $params item]
        :observeItem completed $item
        if {[dict get $item type] eq "agentMessage"} {
            :emit [dict create type text text "\n\n"]
        }
        if {[dict get $item type] eq "mcpToolCall"} {
            incr :tools
            if {${:tools} > 32} {
                error "Question tool-call limit reached"
            }
            set arguments {}
            # Keep the complete JSON type information in the result.
            set triple [ns_json parse -maxdepth 64 -maxstring 8388608 -maxcontainer 100000 -output triples -- $line]
            set arguments [ns_json value -- [list {} [ns_json triples gettype -path {params item arguments} -- $triple] [ns_json triples getvalue -output triples -path {params item arguments} -- $triple]]]
            set itemTriple [list {} object [ns_json triples getvalue -output triples -path {params item} -- $triple]]
            set result [${:backend} toolCallResult $itemTriple]
            set truncated false
            if {[string bytelength $result] > [${:backend} cget -maxResultBytes]} {
                set result {Result omitted from this page because it exceeds the configured display limit.}
                set truncated true
            }
            :emit [dict merge [dict create type tool name [dict get $item tool] status [dict get $item status]  arguments $arguments result $result truncated $truncated] [:toolTiming $item $itemTriple]]
        }
    }
    thread/tokenUsage/updated {
        if {[dict exists $params tokenUsage total]
            && [dict exists $params threadId]
            && (${:thread} eq "" || [dict get $params threadId] eq ${:thread})} {
            # Ignore late notifications from another turn on this thread.
            if {${:usageActive} && ${:turn} ne "" && [dict exists $params turnId]
                && [dict get $params turnId] ne ${:turn}} {
                return
            }
            set counters [::nsmcp::questionUsageCounters [dict get $params tokenUsage total] {
                inputTokens inputTokens cachedInputTokens cachedInputTokens
                cacheWriteInputTokens cacheWriteInputTokens outputTokens outputTokens
                reasoningOutputTokens reasoningOutputTokens totalTokens totalTokens
            }]
            if {$counters ne ""} {
                if {${:usageActive} && ${:turn} eq ""} {
                    # turn/start can emit usage before its RPC reply.
                    set :usagePending [dict create thread [dict get $params threadId] counters $counters]
                    if {[dict exists $params turnId]} {
                        dict set :usagePending turn [dict get $params turnId]
                    }
                } else {
                    :recordUsage [dict get $params threadId] $counters
                }
            }
        }
    }
    turn/completed {
        set :finished [dict get $params turn]
    }
}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: