nsmcp::QuestionService object method createBackend (protected)

 nsmcp::QuestionService[i] createBackend backend moduleDirectory object

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

Parameters:
backend (required)
moduleDirectory (required)
object (required)

Testcases:
No testcase defined.
Source code:
set tokenFile [ns_config ${:section} questionMcpTokenFile {}]
if {$tokenFile eq "" || [file pathtype $tokenFile] ne "absolute" || ![file isfile $tokenFile] || ![file readable $tokenFile]} {
    error "questionMcpTokenFile must name a readable absolute MCP credential file"
}
set timeout [ns_baseunit -time [ns_config ${:section} questionTimeout 180s]]
set maxBytes [ns_baseunit -size [ns_config ${:section} questionMaxBytes 2MiB]]
set maxResult [ns_baseunit -size [ns_config ${:section} questionMaxResultBytes 128KiB]]
if {$timeout < 1 || $timeout > 3600 || $maxResult < 1024} {
    error "Questions require a 1..3600s timeout, result limit of at least 1KiB"
}
set field [expr {$backend eq "codex" ? "questionCodexModel" : "questionApiModel"}]
set model [ns_config ${:section} $field {}]
if {$model eq "" && $backend eq [ns_config ${:section} questionBackend codex]} {
    set model [ns_config ${:section} questionModel {}]
}
switch -- $backend {
    codex {
        set outputTokens [ns_config -int ${:section} questionCodexToolOutputTokens 65536]
        if {$outputTokens<1024 || $outputTokens>262144} {
            error "questionCodexToolOutputTokens must be between 1024 and 262144"
        }
        set initializeTimeout [ns_baseunit -time [ns_config ${:section} questionCodexInitializeTimeout 30s]]
        if {$initializeTimeout < 1 || $initializeTimeout > 3600} {
            error "questionCodexInitializeTimeout must be between 1s and 3600s"
        }
        set directory [ns_config ${:section} questionDirectory [file join [ns_info home] data nsmcp-questions]]
        file mkdir $directory
        set url [ns_config ${:section} questionMcpUrl {}]
        if {![regexp {^https?://[^/]+/} $url]} {
            error "questionMcpUrl must name an absolute HTTP(S) MCP endpoint"
        }
        set host [ns_config ${:section} questionMcpHost {}]
        if {$host ne "" && ![regexp {^[A-Za-z0-9.\[\]:_-]+$} $host]} {
            error "questionMcpHost must be an HTTP authority without whitespace or a path"
        }
        set command [ns_config ${:section} questionCodexCommand codex]
        set bridgeHost [ns_config ${:section} questionCodexBridgeHost {}]
        set bridgePort [ns_config -int -min 1 -max 65535 ${:section} questionCodexBridgePort 8766]
        set bridgeSecretFile [ns_config ${:section} questionCodexBridgeSecretFile {}]
        set bridgeDirectory [ns_config ${:section} questionCodexBridgeDirectory /work]
        if {$bridgeHost eq ""} {
            if {[auto_execok $command] eq ""} {
                error "questionCodexCommand is not executable: $command"
            }
        } else {
            if {![regexp {^[A-Za-z0-9_.:-]+$} $bridgeHost]} {
                error "Invalid questionCodexBridgeHost"
            }
            if {[file pathtype $bridgeSecretFile] ne "absolute" || ![file isfile $bridgeSecretFile] || ![file readable $bridgeSecretFile]} {
                error "questionCodexBridgeSecretFile requires a readable absolute file"
            }
            if {[file pathtype $bridgeDirectory] ne "absolute"} {
                error "questionCodexBridgeDirectory must be absolute"
            }
        }
        set launcher [file join $moduleDirectory scripts codex-questions.sh]
        ::nsmcp::CodexQuestionBackend create $object  -command $command -launcher $launcher  -bridgeHost $bridgeHost -bridgePort $bridgePort -bridgeSecretFile $bridgeSecretFile -bridgeDirectory $bridgeDirectory  -tokenFile $tokenFile -url $url -host $host -directory $directory  -model $model -timeout $timeout -initializeTimeout $initializeTimeout -toolOutputTokens $outputTokens -maxResultBytes $maxResult
    }
    chat-completions {
        set apiKeyFile [ns_config ${:section} questionApiKeyFile {}]
        if {[file pathtype $apiKeyFile] ne "absolute" || ![file isfile $apiKeyFile] || ![file readable $apiKeyFile]} {
            error "questionApiKeyFile must name a readable absolute API key file"
        }
        if {$model eq ""} {
            error "questionModel is required for chat-completions"
        }
        set url [ns_config ${:section} questionApiUrl {}]
        if {[catch {
            ns_parseurl $url
        } parsed] || ![dict exists $parsed proto] || ![dict exists $parsed host]
            || [dict exists $parsed userinfo] || [dict exists $parsed query] || [dict exists $parsed fragment]
            || ([dict get $parsed proto] ne "https"
                && !([dict get $parsed proto] eq "http" && [dict get $parsed host] in {127.0.0.1 ::1 localhost}))} {
            error "questionApiUrl must be HTTPS (or loopback HTTP), without credentials, query or fragment"
        }
        set options [ns_json parse -output triples -maxdepth 16 -maxstring 16384 -maxcontainer 1000 -- [ns_config ${:section} questionApiOptions {{}}]]
        if {[lindex $options 1] ne "object"} {
            error "questionApiOptions must be a JSON object"
        }
        foreach {key type value} [lindex $options 2] {
            if {$key in {model messages tools tool_choice stream stream_options max_tokens max_completion_tokens n}} {
                error "questionApiOptions cannot override $key"
            }
        }
        set maxResponse [ns_baseunit -size [ns_config ${:section} questionMaxResponseBytes 8MiB]]
        if {$maxResponse < 1024} {
            error "questionMaxResponseBytes must be at least 1KiB"
        }
        ::nsmcp::ChatCompletionsBackend create $object  -apiUrl $url -apiKeyFile $apiKeyFile -tokenFile $tokenFile -model $model -apiOptions $options  -timeout $timeout -maxResultBytes $maxResult -maxHistoryBytes [expr {$maxBytes/2}]  -maxResponseBytes $maxResponse  -maxOutputTokens [ns_config -int -min 1 -max 131072 ${:section} questionMaxOutputTokens 4096]  -maxToolCalls [ns_config -int -min 1 -max 128 ${:section} questionMaxToolCalls 32]  -maxApiRequests [ns_config -int -min 1 -max 64 ${:section} questionMaxApiRequests 16]
    }
}
return $object
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: