fix: OpenRouter 402 — send max_tokens cost cap; make session window configurable
gates / consistency-and-conventions (push) Successful in 32s
gates / consistency-and-conventions (push) Successful in 32s
User hit 'API error 402: ... You requested up to 131072 tokens, but can only afford 4511' on the assist alias: no provider ever sent max_tokens, so OpenRouter's credit pre-check billed the routed model's full worst-case output; user also asked to bound session history to the last 5 requests/responses. Architect decisions: - AI_MAX_TOKENS (num, default 2048): sent as max_tokens on OpenRouter and generationConfig.maxOutputTokens on Gemini — a real per-request cost ceiling. llamacpp unchanged (local/free, no pre-check). - AI_SESSION_TURNS (num, default 40 kept back-compat; messages, 2 per exchange — 10 = last 5 conversations): resolved lazily in session_push because config loads after the hardcoded line-25 default. - Both registered in the bin/pos-ai POS_CONFIG @General section, so they appear in 'pos config ai' with num: validation. Reviewer hardening (CHANGES_REQUIRED -> fixed): unguarded env input could reach jq tonumber (0/-5/010/abc all savable via config-ui's ^-?[0-9]+$) and abort the CLI; both providers and session_push now guard with ^[1-9][0-9]*$ and fall back to the default. Verified: fake-curl shim smoke (16 provider-body + 12 session-window checks incl. the 010-regression proof), make gen idempotent, make check OK, make lint 0 FAIL/0 WARN, make test 17 files / 299 checks / 0 fail (~49s), bash -n clean, git diff --check clean. Reviewer ACCEPT (twice). Tester regression round (permanent provider-body + session-pruning coverage) intentionally not run this cycle — user's call; remains a documented follow-up.
This commit is contained in:
@@ -14,8 +14,11 @@ provider_default_model() { printf 'gemini-2.5-flash'; }
|
||||
provider_generate() {
|
||||
local model="$1" messages="$2" system="${3:-}" body resp code body_out errmsg
|
||||
# Convert OpenAI messages format to Gemini contents format
|
||||
body="$(printf '%s' "$messages" | jq -c '{
|
||||
contents: [.messages[]? | {role: (.role | gsub("assistant";"model")), parts: [{text: .content}]}]
|
||||
local mt="${AI_MAX_TOKENS:-2048}"
|
||||
[[ "$mt" =~ ^[1-9][0-9]*$ ]] || mt=2048
|
||||
body="$(printf '%s' "$messages" | jq -c --arg mt "$mt" '{
|
||||
contents: [.messages[]? | {role: (.role | gsub("assistant";"model")), parts: [{text: .content}]}],
|
||||
generationConfig: {maxOutputTokens: ($mt|tonumber)}
|
||||
}')"
|
||||
if [ -n "$system" ]; then
|
||||
body="$(printf '%s' "$body" | jq -c --arg s "$system" \
|
||||
|
||||
@@ -19,8 +19,10 @@ provider_generate() {
|
||||
else
|
||||
body="$(printf '%s' "$messages" | jq -c '.messages')"
|
||||
fi
|
||||
body="$(printf '%s' "$body" | jq -nc --arg m "$model" --argjson msgs "$body" \
|
||||
'{model:$m, messages:$msgs}')"
|
||||
local mt="${AI_MAX_TOKENS:-2048}"
|
||||
[[ "$mt" =~ ^[1-9][0-9]*$ ]] || mt=2048
|
||||
body="$(printf '%s' "$body" | jq -nc --arg m "$model" --argjson msgs "$body" --arg mt "$mt" \
|
||||
'{model:$m, messages:$msgs, max_tokens:($mt|tonumber)}')"
|
||||
resp="$(curl -sS -m 60 -X POST "https://openrouter.ai/api/v1/chat/completions" \
|
||||
-H "Authorization: Bearer ${AI_API_KEY}" \
|
||||
-H "Content-Type: application/json" \
|
||||
|
||||
Reference in New Issue
Block a user