fix(qoder): report usage to all clients and stop inlining large attachments

- Coalesce Qoder's empty finish-in-delta frame with the later choices:[] usage
  frame so OpenAI and Claude clients receive prompt_tokens, completion_tokens
  and cache-hit tokens (the dashboard already saw them)
- Upload inlined images through /api/v2/image/upload like qodercli, and stub
  oversized non-image files instead of stuffing 30MB+ data URIs into
  agent_chat_generation
- Emit response.completed -> response.usage for chat-native upstreams so
  /v1/responses clients (Codex CLI, sub2api) no longer log 0/0/0
- Keep Claude message_delta.usage working when usage arrives without choices[0]
- Escalate to the smallest advertised Qoder context tier (200K/400K/1M) when
  the estimated prompt no longer fits max_input_tokens
- Pass apiKey for PAT connections and list hidden enable:false catalog keys
  from /v1/models
This commit is contained in:
LLL
2026-09-10 22:06:49 +07:00
committed by decolua
parent 832a34659e
commit 1f10f9e5c4
19 changed files with 1615 additions and 121 deletions

View File

@@ -67,3 +67,34 @@ export function toOpenAIUsage(raw, kind) {
if (!extract || !raw || typeof raw !== "object") return null;
return buildUsage(extract(raw));
}
// Convert an OpenAI-shaped (or already-canonical / Claude-shaped) usage object into the
// Responses API shape emitted by `response.completed`. Details objects are always present
// (like the real API) so proxies that read `input_tokens_details.cached_tokens` never see undefined.
// Returns null when there is nothing countable.
export function toResponsesUsage(usage) {
if (!usage || typeof usage !== "object") return null;
const input = n(usage.prompt_tokens ?? usage.input_tokens);
const output = n(usage.completion_tokens ?? usage.output_tokens);
if (input === 0 && output === 0) return null;
const cached = n(
usage.input_tokens_details?.cached_tokens ??
usage.prompt_tokens_details?.cached_tokens ??
usage.cached_tokens ??
usage.cache_read_input_tokens
);
const reasoning = n(
usage.output_tokens_details?.reasoning_tokens ??
usage.completion_tokens_details?.reasoning_tokens ??
usage.reasoning_tokens
);
const out = {
input_tokens: input,
output_tokens: output,
total_tokens: typeof usage.total_tokens === "number" ? usage.total_tokens : input + output,
input_tokens_details: { cached_tokens: cached },
output_tokens_details: { reasoning_tokens: reasoning },
};
if (usage.estimated) out.estimated = true;
return out;
}