mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
fix(ai): retry chat requests with reasoning_effort none when a server refuses tools with reasoning
Azure AI Foundry reasoning deployments behind an "Own server" connection reject function tools on /v1/chat/completions at their default reasoning effort and answer HTTP 400 naming reasoning_effort. The Responses API switch from #2792 only covers direct OpenAI keys, so the assistant could not run on these deployments at all. The chat client still never sends reasoning_effort, so servers that do not know the field (Ollama, LM Studio, vLLM, OpenRouter) see unchanged requests. When a request with tools is refused over reasoning_effort, it goes out once more with reasoning_effort "none", and the rest of the turn keeps sending it. The refused field is now carried on AiProviderError instead of only being logged. Fixes #3043
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { logWarn } from '../../audit/audit-log.logger';
|
||||
import type {
|
||||
AiChatClient,
|
||||
AiChatEvent,
|
||||
@@ -8,7 +9,7 @@ import type {
|
||||
AiSource,
|
||||
AiToolCall,
|
||||
} from '../ai-chat-client.interface';
|
||||
import { AiProviderError, errorCodeOf, providerFailure } from './provider-errors';
|
||||
import { AiProviderError, errorCodeOf, isReasoningEffortRefusal, providerFailure } from './provider-errors';
|
||||
import {
|
||||
asCount,
|
||||
asRecord,
|
||||
@@ -107,6 +108,13 @@ const FINISH_REASONS: Record<string, AiFinishReason> = { tool_calls: 'tool-calls
|
||||
* schemas, also when an admin enters its endpoint as their own server, and
|
||||
* OpenAI the prompt cache key, which the others do not know. Gemini's thought
|
||||
* signatures come back as the turn's provider state.
|
||||
*
|
||||
* No request names a reasoning effort, since a server that does not reason may
|
||||
* refuse the field. A reasoning model that takes function tools only without
|
||||
* reasoning (Azure AI Foundry's deployments, #3043) refuses the default effort
|
||||
* by naming `reasoning_effort`; the request then goes out once more with
|
||||
* `reasoning_effort: "none"`, and so does every later request with tools on
|
||||
* this client, which lives for one turn.
|
||||
*/
|
||||
export class OpenAiChatClient implements AiChatClient {
|
||||
/** OpenRouter searches the web for any model through its web plugin; the others have no search here. */
|
||||
@@ -117,6 +125,9 @@ export class OpenAiChatClient implements AiChatClient {
|
||||
/** The kind whose tool schemas the endpoint takes. */
|
||||
private readonly schemaKind: AiConnectionKind;
|
||||
|
||||
/** Whether the server refused function tools with the model's reasoning, so they go out without it. */
|
||||
private toolsWithoutReasoning = false;
|
||||
|
||||
constructor(
|
||||
readonly kind: AiConnectionKind,
|
||||
readonly model: string,
|
||||
@@ -128,42 +139,7 @@ export class OpenAiChatClient implements AiChatClient {
|
||||
}
|
||||
|
||||
async *stream(request: AiChatRequest, signal: AbortSignal): AsyncGenerator<AiChatEvent> {
|
||||
const body = {
|
||||
model: this.model,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
messages: toChatMessages(request, this.model),
|
||||
...(request.tools.length > 0
|
||||
? {
|
||||
tools: request.tools.map((tool) => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: toProviderSchema(tool.parameters, this.schemaKind),
|
||||
},
|
||||
})),
|
||||
tool_choice: request.toolChoice ?? 'auto',
|
||||
}
|
||||
: {}),
|
||||
...(this.kind === 'openai' && request.cacheKey ? { prompt_cache_key: request.cacheKey } : {}),
|
||||
...(request.webSearch && this.webSearch ? { plugins: [{ id: 'web', max_results: 5 }] } : {}),
|
||||
};
|
||||
const events = await openProviderStream(
|
||||
this.fetcher,
|
||||
this.kind,
|
||||
`${this.baseUrl}/chat/completions`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
accept: 'text/event-stream',
|
||||
...(this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
signal,
|
||||
);
|
||||
const events = await this.open(request, signal);
|
||||
|
||||
const turn = new ChatTurn();
|
||||
for await (const sse of readSseEvents(events)) {
|
||||
@@ -192,6 +168,61 @@ export class OpenAiChatClient implements AiChatClient {
|
||||
}
|
||||
yield { type: 'finish', reason: turn.finishReason() };
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the request's stream. A refusal of the model's reasoning effort for
|
||||
* a request with tools is answered once by asking again without reasoning.
|
||||
*/
|
||||
private async open(request: AiChatRequest, signal: AbortSignal): Promise<ReadableStream<Uint8Array>> {
|
||||
try {
|
||||
return await this.send(request, signal);
|
||||
} catch (err) {
|
||||
if (this.toolsWithoutReasoning || request.tools.length === 0 || !isReasoningEffortRefusal(err)) throw err;
|
||||
this.toolsWithoutReasoning = true;
|
||||
logWarn(`[ai] ${this.kind} takes function tools only without reasoning: asking again with reasoning_effort none`);
|
||||
return this.send(request, signal);
|
||||
}
|
||||
}
|
||||
|
||||
private send(request: AiChatRequest, signal: AbortSignal): Promise<ReadableStream<Uint8Array>> {
|
||||
const body = {
|
||||
model: this.model,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
messages: toChatMessages(request, this.model),
|
||||
...(request.tools.length > 0
|
||||
? {
|
||||
tools: request.tools.map((tool) => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: toProviderSchema(tool.parameters, this.schemaKind),
|
||||
},
|
||||
})),
|
||||
tool_choice: request.toolChoice ?? 'auto',
|
||||
}
|
||||
: {}),
|
||||
...(this.toolsWithoutReasoning && request.tools.length > 0 ? { reasoning_effort: 'none' } : {}),
|
||||
...(this.kind === 'openai' && request.cacheKey ? { prompt_cache_key: request.cacheKey } : {}),
|
||||
...(request.webSearch && this.webSearch ? { plugins: [{ id: 'web', max_results: 5 }] } : {}),
|
||||
};
|
||||
return openProviderStream(
|
||||
this.fetcher,
|
||||
this.kind,
|
||||
`${this.baseUrl}/chat/completions`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
accept: 'text/event-stream',
|
||||
...(this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** A tool call while it streams in: its id, name and arguments so far, and Gemini's signature. */
|
||||
|
||||
@@ -10,6 +10,12 @@ export const CHATGPT_USAGE_URL = 'https://chatgpt.com/settings/usage';
|
||||
* request again later can help.
|
||||
*/
|
||||
export class AiProviderError extends Error {
|
||||
/**
|
||||
* The request field the provider named as refused (OpenAI's `param`), when
|
||||
* it named one as a bare path. Set by `refusalOf` for an HTTP refusal.
|
||||
*/
|
||||
refusedParam?: string;
|
||||
|
||||
constructor(
|
||||
readonly code: AssistantErrorCode,
|
||||
readonly status: number,
|
||||
@@ -120,3 +126,12 @@ export function namesImageInput(status: number, body: unknown): boolean {
|
||||
export function isImageRefusal(err: unknown): boolean {
|
||||
return err instanceof AiProviderError && err.providerCode === IMAGE_INPUT_REFUSED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a provider error is the refusal of the request's reasoning effort:
|
||||
* a reasoning model behind Chat Completions that takes function tools only
|
||||
* with `reasoning_effort: "none"`, as Azure AI Foundry's deployments do (#3043).
|
||||
*/
|
||||
export function isReasoningEffortRefusal(err: unknown): boolean {
|
||||
return err instanceof AiProviderError && err.status === 400 && err.refusedParam === 'reasoning_effort';
|
||||
}
|
||||
|
||||
@@ -65,7 +65,9 @@ export async function refusalOf(kind: AiConnectionKind, res: Response): Promise<
|
||||
logWarn(
|
||||
`[ai] ${kind} refused the request: HTTP ${res.status}${code ? ` (${code})` : ''}${param ? ` at ${param}` : ''}`,
|
||||
);
|
||||
return providerFailure(kind, res.status, code);
|
||||
const failure = providerFailure(kind, res.status, code);
|
||||
failure.refusedParam = param;
|
||||
return failure;
|
||||
}
|
||||
|
||||
/** The field an error body names as refused (OpenAI's `param`, such as `input[12]`), when it is a bare path. */
|
||||
|
||||
Reference in New Issue
Block a user