Compare commits
1 Commits
fix/issue-
...
fix/issue-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
effa6ef83d |
@@ -111,7 +111,7 @@ import { BackgroundTasksDialog } from '../tasks/BackgroundTasksDialog.js';
|
|||||||
import { shouldHideTasksFooter } from '../tasks/taskStatusUtils.js';
|
import { shouldHideTasksFooter } from '../tasks/taskStatusUtils.js';
|
||||||
import { TeamsDialog } from '../teams/TeamsDialog.js';
|
import { TeamsDialog } from '../teams/TeamsDialog.js';
|
||||||
import VimTextInput from '../VimTextInput.js';
|
import VimTextInput from '../VimTextInput.js';
|
||||||
import { detectModeEntry, getModeFromInput, getValueFromInput } from './inputModes.js';
|
import { getModeFromInput, getValueFromInput } from './inputModes.js';
|
||||||
import { FOOTER_TEMPORARY_STATUS_TIMEOUT, Notifications } from './Notifications.js';
|
import { FOOTER_TEMPORARY_STATUS_TIMEOUT, Notifications } from './Notifications.js';
|
||||||
import PromptInputFooter from './PromptInputFooter.js';
|
import PromptInputFooter from './PromptInputFooter.js';
|
||||||
import type { SuggestionItem } from './PromptInputFooterSuggestions.js';
|
import type { SuggestionItem } from './PromptInputFooterSuggestions.js';
|
||||||
@@ -878,22 +878,24 @@ function PromptInput({
|
|||||||
abortPromptSuggestion();
|
abortPromptSuggestion();
|
||||||
abortSpeculation(setAppState);
|
abortSpeculation(setAppState);
|
||||||
|
|
||||||
// Strip the mode character from the buffer when entering bash mode — the
|
// Check if this is a single character insertion at the start
|
||||||
// mode itself is shown via the prompt prefix in the UI. Without this,
|
const isSingleCharInsertion = value.length === input.length + 1;
|
||||||
// typing `!` into empty input would enter bash mode but leave the literal
|
const insertedAtStart = cursorOffset === 0;
|
||||||
// `!` in the buffer (issue #662).
|
const mode = getModeFromInput(value);
|
||||||
const modeEntry = detectModeEntry({
|
if (insertedAtStart && mode !== 'prompt') {
|
||||||
value,
|
if (isSingleCharInsertion) {
|
||||||
prevInputLength: input.length,
|
onModeChange(mode);
|
||||||
cursorOffset,
|
return;
|
||||||
});
|
}
|
||||||
if (modeEntry) {
|
// Multi-char insertion into empty input (e.g. tab-accepting "! gcloud auth login")
|
||||||
onModeChange(modeEntry.mode);
|
if (input.length === 0) {
|
||||||
const cleaned = modeEntry.strippedValue.replaceAll('\t', ' ');
|
onModeChange(mode);
|
||||||
pushToBuffer(input, cursorOffset, pastedContents);
|
const valueWithoutMode = getValueFromInput(value).replaceAll('\t', ' ');
|
||||||
trackAndSetInput(cleaned);
|
pushToBuffer(input, cursorOffset, pastedContents);
|
||||||
setCursorOffset(cleaned.length);
|
trackAndSetInput(valueWithoutMode);
|
||||||
return;
|
setCursorOffset(valueWithoutMode.length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const processedValue = value.replaceAll('\t', ' ');
|
const processedValue = value.replaceAll('\t', ' ');
|
||||||
|
|
||||||
|
|||||||
@@ -1,104 +0,0 @@
|
|||||||
import { describe, expect, it } from 'bun:test'
|
|
||||||
import {
|
|
||||||
detectModeEntry,
|
|
||||||
getModeFromInput,
|
|
||||||
getValueFromInput,
|
|
||||||
isInputModeCharacter,
|
|
||||||
prependModeCharacterToInput,
|
|
||||||
} from './inputModes.js'
|
|
||||||
|
|
||||||
describe('inputModes', () => {
|
|
||||||
describe('getModeFromInput', () => {
|
|
||||||
it('returns bash mode for input starting with !', () => {
|
|
||||||
expect(getModeFromInput('!')).toBe('bash')
|
|
||||||
expect(getModeFromInput('!ls')).toBe('bash')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns prompt mode for non-bash input', () => {
|
|
||||||
expect(getModeFromInput('')).toBe('prompt')
|
|
||||||
expect(getModeFromInput('hello')).toBe('prompt')
|
|
||||||
expect(getModeFromInput(' !')).toBe('prompt')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('getValueFromInput', () => {
|
|
||||||
it('strips the leading ! when entering bash mode', () => {
|
|
||||||
expect(getValueFromInput('!')).toBe('')
|
|
||||||
expect(getValueFromInput('!ls -la')).toBe('ls -la')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns input unchanged in prompt mode', () => {
|
|
||||||
expect(getValueFromInput('')).toBe('')
|
|
||||||
expect(getValueFromInput('hello')).toBe('hello')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('isInputModeCharacter', () => {
|
|
||||||
it('returns true only for the bare ! character', () => {
|
|
||||||
expect(isInputModeCharacter('!')).toBe(true)
|
|
||||||
expect(isInputModeCharacter('!ls')).toBe(false)
|
|
||||||
expect(isInputModeCharacter('')).toBe(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('prependModeCharacterToInput', () => {
|
|
||||||
it('prepends ! when mode is bash', () => {
|
|
||||||
expect(prependModeCharacterToInput('ls', 'bash')).toBe('!ls')
|
|
||||||
expect(prependModeCharacterToInput('', 'bash')).toBe('!')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns input unchanged in prompt mode', () => {
|
|
||||||
expect(prependModeCharacterToInput('hello', 'prompt')).toBe('hello')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('detectModeEntry', () => {
|
|
||||||
// Regression for #662 — typing `!` into empty input must switch to bash
|
|
||||||
// mode AND yield an empty stripped buffer. Before the fix the single-char
|
|
||||||
// path returned without stripping, leaving `!` visible in the buffer.
|
|
||||||
it('strips the mode character when typing ! into empty input', () => {
|
|
||||||
expect(
|
|
||||||
detectModeEntry({ value: '!', prevInputLength: 0, cursorOffset: 0 }),
|
|
||||||
).toEqual({ mode: 'bash', strippedValue: '' })
|
|
||||||
})
|
|
||||||
|
|
||||||
it('strips the mode character when pasting !cmd into empty input', () => {
|
|
||||||
expect(
|
|
||||||
detectModeEntry({ value: '!ls -la', prevInputLength: 0, cursorOffset: 0 }),
|
|
||||||
).toEqual({ mode: 'bash', strippedValue: 'ls -la' })
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns null when the cursor is not at the start', () => {
|
|
||||||
expect(
|
|
||||||
detectModeEntry({ value: '!', prevInputLength: 0, cursorOffset: 1 }),
|
|
||||||
).toBeNull()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns null when the value does not start with !', () => {
|
|
||||||
expect(
|
|
||||||
detectModeEntry({ value: 'hello', prevInputLength: 0, cursorOffset: 0 }),
|
|
||||||
).toBeNull()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns null when typing ! after existing text', () => {
|
|
||||||
// value="ab!" with prevInputLength=2 is a single-char insertion but does
|
|
||||||
// not start with ! — getModeFromInput returns 'prompt'.
|
|
||||||
expect(
|
|
||||||
detectModeEntry({ value: 'ab!', prevInputLength: 2, cursorOffset: 0 }),
|
|
||||||
).toBeNull()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns null when prepending ! to non-empty existing text', () => {
|
|
||||||
// Single-char insertion at start that produces "!ab" from "ab" — value
|
|
||||||
// length is 3, prevInputLength is 2, so isSingleCharInsertion is true
|
|
||||||
// and isMultiCharIntoEmpty is false. We accept the mode change here so
|
|
||||||
// that typing ! at the start of existing text still toggles mode.
|
|
||||||
const result = detectModeEntry({
|
|
||||||
value: '!ab',
|
|
||||||
prevInputLength: 2,
|
|
||||||
cursorOffset: 0,
|
|
||||||
})
|
|
||||||
expect(result).toEqual({ mode: 'bash', strippedValue: 'ab' })
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -31,30 +31,3 @@ export function getValueFromInput(input: string): string {
|
|||||||
export function isInputModeCharacter(input: string): boolean {
|
export function isInputModeCharacter(input: string): boolean {
|
||||||
return input === '!'
|
return input === '!'
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ModeEntryDecision = {
|
|
||||||
mode: HistoryMode
|
|
||||||
strippedValue: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decide whether an onChange `value` should switch the input mode (e.g.
|
|
||||||
* `prompt` → `bash`) and what the stripped buffer value should be.
|
|
||||||
*
|
|
||||||
* Returns null when no mode change applies. Returns a decision otherwise so
|
|
||||||
* callers run a single update path — no separate single-char vs multi-char
|
|
||||||
* branches that can drift apart.
|
|
||||||
*/
|
|
||||||
export function detectModeEntry(args: {
|
|
||||||
value: string
|
|
||||||
prevInputLength: number
|
|
||||||
cursorOffset: number
|
|
||||||
}): ModeEntryDecision | null {
|
|
||||||
if (args.cursorOffset !== 0) return null
|
|
||||||
const mode = getModeFromInput(args.value)
|
|
||||||
if (mode === 'prompt') return null
|
|
||||||
const isSingleCharInsertion = args.value.length === args.prevInputLength + 1
|
|
||||||
const isMultiCharIntoEmpty = args.prevInputLength === 0
|
|
||||||
if (!isSingleCharInsertion && !isMultiCharIntoEmpty) return null
|
|
||||||
return { mode, strippedValue: getValueFromInput(args.value) }
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,6 +28,38 @@ test('maps endpoint_not_found category markers to actionable setup guidance', ()
|
|||||||
expect(text).toContain('/v1')
|
expect(text).toContain('/v1')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('endpoint_not_found from a remote host shows the actual host, not Ollama (issue #926)', () => {
|
||||||
|
const error = APIError.generate(
|
||||||
|
404,
|
||||||
|
undefined,
|
||||||
|
'OpenAI API error 404: Not Found [openai_category=endpoint_not_found,host=integrate.api.nvidia.com] Hint: Endpoint at integrate.api.nvidia.com returned 404.',
|
||||||
|
new Headers(),
|
||||||
|
)
|
||||||
|
|
||||||
|
const message = getAssistantMessageFromError(error, 'moonshotai/kimi-k2.5-thinking')
|
||||||
|
const text = getFirstText(message)
|
||||||
|
|
||||||
|
expect(text).toContain('integrate.api.nvidia.com')
|
||||||
|
expect(text).toContain('moonshotai/kimi-k2.5-thinking')
|
||||||
|
expect(text).not.toContain('Ollama')
|
||||||
|
expect(text).not.toContain('11434')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('endpoint_not_found without a host falls back to the Ollama-aware message', () => {
|
||||||
|
const error = APIError.generate(
|
||||||
|
404,
|
||||||
|
undefined,
|
||||||
|
'OpenAI API error 404: Not Found [openai_category=endpoint_not_found] Hint: Confirm OPENAI_BASE_URL includes /v1.',
|
||||||
|
new Headers(),
|
||||||
|
)
|
||||||
|
|
||||||
|
const message = getAssistantMessageFromError(error, 'qwen2.5-coder:7b')
|
||||||
|
const text = getFirstText(message)
|
||||||
|
|
||||||
|
expect(text).toContain('Provider endpoint was not found')
|
||||||
|
expect(text).toContain('Ollama')
|
||||||
|
})
|
||||||
|
|
||||||
test('maps tool_call_incompatible category markers to model/tool guidance', () => {
|
test('maps tool_call_incompatible category markers to model/tool guidance', () => {
|
||||||
const error = APIError.generate(
|
const error = APIError.generate(
|
||||||
400,
|
400,
|
||||||
|
|||||||
@@ -51,7 +51,9 @@ import {
|
|||||||
import { shouldProcessRateLimits } from '../rateLimitMocking.js' // Used for /mock-limits command
|
import { shouldProcessRateLimits } from '../rateLimitMocking.js' // Used for /mock-limits command
|
||||||
import { extractConnectionErrorDetails, formatAPIError } from './errorUtils.js'
|
import { extractConnectionErrorDetails, formatAPIError } from './errorUtils.js'
|
||||||
import {
|
import {
|
||||||
|
extractOpenAICategoryHost,
|
||||||
extractOpenAICategoryMarker,
|
extractOpenAICategoryMarker,
|
||||||
|
isLocalhostLikeHost,
|
||||||
type OpenAICompatibilityFailureCategory,
|
type OpenAICompatibilityFailureCategory,
|
||||||
} from './openaiErrorClassification.js'
|
} from './openaiErrorClassification.js'
|
||||||
|
|
||||||
@@ -68,25 +70,29 @@ function mapOpenAICompatibilityFailureToAssistantMessage(options: {
|
|||||||
category: OpenAICompatibilityFailureCategory
|
category: OpenAICompatibilityFailureCategory
|
||||||
model: string
|
model: string
|
||||||
rawMessage: string
|
rawMessage: string
|
||||||
|
host?: string
|
||||||
}): AssistantMessage {
|
}): AssistantMessage {
|
||||||
const switchCmd = getIsNonInteractiveSession() ? '--model' : '/model'
|
const switchCmd = getIsNonInteractiveSession() ? '--model' : '/model'
|
||||||
const compactHint = getIsNonInteractiveSession()
|
const compactHint = getIsNonInteractiveSession()
|
||||||
? 'Reduce prompt size or start a new session.'
|
? 'Reduce prompt size or start a new session.'
|
||||||
: 'Run /compact or start a new session with /new.'
|
: 'Run /compact or start a new session with /new.'
|
||||||
|
const isLocalhost = options.host === undefined || isLocalhostLikeHost(options.host)
|
||||||
|
|
||||||
switch (options.category) {
|
switch (options.category) {
|
||||||
case 'localhost_resolution_failed':
|
case 'localhost_resolution_failed':
|
||||||
case 'connection_refused':
|
case 'connection_refused':
|
||||||
return createAssistantAPIErrorMessage({
|
return createAssistantAPIErrorMessage({
|
||||||
content:
|
content: isLocalhost
|
||||||
'Could not connect to the local OpenAI-compatible provider. Ensure the local server is running, then use OPENAI_BASE_URL=http://127.0.0.1:11434/v1 for Ollama.',
|
? 'Could not connect to the local OpenAI-compatible provider. Ensure the local server is running, then use OPENAI_BASE_URL=http://127.0.0.1:11434/v1 for Ollama.'
|
||||||
|
: `Could not connect to the provider at ${options.host}. Verify OPENAI_BASE_URL is correct and that the host is reachable.`,
|
||||||
error: 'unknown',
|
error: 'unknown',
|
||||||
})
|
})
|
||||||
|
|
||||||
case 'endpoint_not_found':
|
case 'endpoint_not_found':
|
||||||
return createAssistantAPIErrorMessage({
|
return createAssistantAPIErrorMessage({
|
||||||
content:
|
content: isLocalhost
|
||||||
'Provider endpoint was not found. Confirm OPENAI_BASE_URL targets an OpenAI-compatible /v1 endpoint (for Ollama: http://127.0.0.1:11434/v1).',
|
? 'Provider endpoint was not found. Confirm OPENAI_BASE_URL targets an OpenAI-compatible /v1 endpoint (for Ollama: http://127.0.0.1:11434/v1).'
|
||||||
|
: `Provider endpoint at ${options.host} returned 404. Verify OPENAI_BASE_URL is correct and that the selected model (${options.model}) is supported by this provider.`,
|
||||||
error: 'invalid_request',
|
error: 'invalid_request',
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -567,6 +573,7 @@ export function getAssistantMessageFromError(
|
|||||||
category: openaiCategory,
|
category: openaiCategory,
|
||||||
model,
|
model,
|
||||||
rawMessage: error.message,
|
rawMessage: error.message,
|
||||||
|
host: extractOpenAICategoryHost(error.message),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import {
|
|||||||
buildOpenAICompatibilityErrorMessage,
|
buildOpenAICompatibilityErrorMessage,
|
||||||
classifyOpenAIHttpFailure,
|
classifyOpenAIHttpFailure,
|
||||||
classifyOpenAINetworkFailure,
|
classifyOpenAINetworkFailure,
|
||||||
|
extractOpenAICategoryHost,
|
||||||
extractOpenAICategoryMarker,
|
extractOpenAICategoryMarker,
|
||||||
formatOpenAICategoryMarker,
|
formatOpenAICategoryMarker,
|
||||||
|
isLocalhostLikeHost,
|
||||||
} from './openaiErrorClassification.js'
|
} from './openaiErrorClassification.js'
|
||||||
|
|
||||||
test('classifies localhost ECONNREFUSED as connection_refused', () => {
|
test('classifies localhost ECONNREFUSED as connection_refused', () => {
|
||||||
@@ -95,3 +97,58 @@ test('ignores unknown category markers during extraction', () => {
|
|||||||
const malformed = 'OpenAI API error 500 [openai_category=totally_fake_category]'
|
const malformed = 'OpenAI API error 500 [openai_category=totally_fake_category]'
|
||||||
expect(extractOpenAICategoryMarker(malformed)).toBeUndefined()
|
expect(extractOpenAICategoryMarker(malformed)).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('endpoint_not_found 404 from a remote host gets a host-aware hint (issue #926)', () => {
|
||||||
|
const failure = classifyOpenAIHttpFailure({
|
||||||
|
status: 404,
|
||||||
|
body: 'Not Found',
|
||||||
|
url: 'https://integrate.api.nvidia.com/v1/chat/completions',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(failure.category).toBe('endpoint_not_found')
|
||||||
|
expect(failure.requestUrl).toBe('https://integrate.api.nvidia.com/v1/chat/completions')
|
||||||
|
expect(failure.hint).toContain('integrate.api.nvidia.com')
|
||||||
|
expect(failure.hint).not.toContain('local providers')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('endpoint_not_found 404 from localhost keeps the Ollama-flavored hint', () => {
|
||||||
|
const failure = classifyOpenAIHttpFailure({
|
||||||
|
status: 404,
|
||||||
|
body: 'Not Found',
|
||||||
|
url: 'http://127.0.0.1:11434/v1/chat/completions',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(failure.category).toBe('endpoint_not_found')
|
||||||
|
expect(failure.hint).toContain('local providers')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('marker round-trip preserves host segment', () => {
|
||||||
|
const formatted = buildOpenAICompatibilityErrorMessage(
|
||||||
|
'OpenAI API error 404: Not Found',
|
||||||
|
{
|
||||||
|
category: 'endpoint_not_found',
|
||||||
|
hint: 'Endpoint at integrate.api.nvidia.com returned 404.',
|
||||||
|
requestUrl: 'https://integrate.api.nvidia.com/v1/chat/completions',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(formatted).toContain('[openai_category=endpoint_not_found,host=integrate.api.nvidia.com]')
|
||||||
|
expect(extractOpenAICategoryMarker(formatted)).toBe('endpoint_not_found')
|
||||||
|
expect(extractOpenAICategoryHost(formatted)).toBe('integrate.api.nvidia.com')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('marker without host stays backward-compatible', () => {
|
||||||
|
const marker = formatOpenAICategoryMarker('endpoint_not_found')
|
||||||
|
expect(marker).toBe('[openai_category=endpoint_not_found]')
|
||||||
|
expect(extractOpenAICategoryMarker(marker)).toBe('endpoint_not_found')
|
||||||
|
expect(extractOpenAICategoryHost(marker)).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('isLocalhostLikeHost matches loopback variants', () => {
|
||||||
|
expect(isLocalhostLikeHost('localhost')).toBe(true)
|
||||||
|
expect(isLocalhostLikeHost('127.0.0.1')).toBe(true)
|
||||||
|
expect(isLocalhostLikeHost('127.0.0.5')).toBe(true)
|
||||||
|
expect(isLocalhostLikeHost('::1')).toBe(true)
|
||||||
|
expect(isLocalhostLikeHost('integrate.api.nvidia.com')).toBe(false)
|
||||||
|
expect(isLocalhostLikeHost(undefined)).toBe(false)
|
||||||
|
})
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export type OpenAICompatibilityFailure = {
|
|||||||
hint?: string
|
hint?: string
|
||||||
code?: string
|
code?: string
|
||||||
status?: number
|
status?: number
|
||||||
|
requestUrl?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const OPENAI_CATEGORY_MARKER_PREFIX = '[openai_category='
|
const OPENAI_CATEGORY_MARKER_PREFIX = '[openai_category='
|
||||||
@@ -96,6 +97,11 @@ function isLocalhostLikeHostname(hostname: string | null): boolean {
|
|||||||
return /^127\./.test(hostname)
|
return /^127\./.test(hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isLocalhostLikeHost(host: string | null | undefined): boolean {
|
||||||
|
if (!host) return false
|
||||||
|
return isLocalhostLikeHostname(host.toLowerCase())
|
||||||
|
}
|
||||||
|
|
||||||
function isContextOverflowMessage(body: string): boolean {
|
function isContextOverflowMessage(body: string): boolean {
|
||||||
const lower = body.toLowerCase()
|
const lower = body.toLowerCase()
|
||||||
return (
|
return (
|
||||||
@@ -149,14 +155,18 @@ function isModelNotFoundMessage(body: string): boolean {
|
|||||||
|
|
||||||
export function formatOpenAICategoryMarker(
|
export function formatOpenAICategoryMarker(
|
||||||
category: OpenAICompatibilityFailureCategory,
|
category: OpenAICompatibilityFailureCategory,
|
||||||
|
host?: string,
|
||||||
): string {
|
): string {
|
||||||
|
if (host && /^[A-Za-z0-9.\-:]+$/.test(host)) {
|
||||||
|
return `${OPENAI_CATEGORY_MARKER_PREFIX}${category},host=${host}]`
|
||||||
|
}
|
||||||
return `${OPENAI_CATEGORY_MARKER_PREFIX}${category}]`
|
return `${OPENAI_CATEGORY_MARKER_PREFIX}${category}]`
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractOpenAICategoryMarker(
|
export function extractOpenAICategoryMarker(
|
||||||
message: string,
|
message: string,
|
||||||
): OpenAICompatibilityFailureCategory | undefined {
|
): OpenAICompatibilityFailureCategory | undefined {
|
||||||
const match = message.match(/\[openai_category=([a-z_]+)]/)
|
const match = message.match(/\[openai_category=([a-z_]+)(?:,host=[^\]]+)?]/)
|
||||||
const category = match?.[1]
|
const category = match?.[1]
|
||||||
|
|
||||||
if (!category || !isOpenAICompatibilityFailureCategory(category)) {
|
if (!category || !isOpenAICompatibilityFailureCategory(category)) {
|
||||||
@@ -166,11 +176,17 @@ export function extractOpenAICategoryMarker(
|
|||||||
return category
|
return category
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function extractOpenAICategoryHost(message: string): string | undefined {
|
||||||
|
const match = message.match(/\[openai_category=[a-z_]+,host=([A-Za-z0-9.\-:]+)]/)
|
||||||
|
return match?.[1]
|
||||||
|
}
|
||||||
|
|
||||||
export function buildOpenAICompatibilityErrorMessage(
|
export function buildOpenAICompatibilityErrorMessage(
|
||||||
baseMessage: string,
|
baseMessage: string,
|
||||||
failure: Pick<OpenAICompatibilityFailure, 'category' | 'hint'>,
|
failure: Pick<OpenAICompatibilityFailure, 'category' | 'hint' | 'requestUrl'>,
|
||||||
): string {
|
): string {
|
||||||
const marker = formatOpenAICategoryMarker(failure.category)
|
const host = failure.requestUrl ? getHostname(failure.requestUrl) ?? undefined : undefined
|
||||||
|
const marker = formatOpenAICategoryMarker(failure.category, host)
|
||||||
const hint = failure.hint ? ` Hint: ${failure.hint}` : ''
|
const hint = failure.hint ? ` Hint: ${failure.hint}` : ''
|
||||||
return `${baseMessage} ${marker}${hint}`
|
return `${baseMessage} ${marker}${hint}`
|
||||||
}
|
}
|
||||||
@@ -247,8 +263,11 @@ export function classifyOpenAINetworkFailure(
|
|||||||
export function classifyOpenAIHttpFailure(options: {
|
export function classifyOpenAIHttpFailure(options: {
|
||||||
status: number
|
status: number
|
||||||
body: string
|
body: string
|
||||||
|
url?: string
|
||||||
}): OpenAICompatibilityFailure {
|
}): OpenAICompatibilityFailure {
|
||||||
const body = options.body ?? ''
|
const body = options.body ?? ''
|
||||||
|
const hostname = options.url ? getHostname(options.url) : null
|
||||||
|
const isLocalHost = isLocalhostLikeHostname(hostname)
|
||||||
|
|
||||||
if (options.status === 401 || options.status === 403) {
|
if (options.status === 401 || options.status === 403) {
|
||||||
return {
|
return {
|
||||||
@@ -284,13 +303,17 @@ export function classifyOpenAIHttpFailure(options: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options.status === 404) {
|
if (options.status === 404) {
|
||||||
|
const isRemote = hostname !== null && !isLocalHost
|
||||||
return {
|
return {
|
||||||
source: 'http',
|
source: 'http',
|
||||||
category: 'endpoint_not_found',
|
category: 'endpoint_not_found',
|
||||||
retryable: false,
|
retryable: false,
|
||||||
status: options.status,
|
status: options.status,
|
||||||
message: body,
|
message: body,
|
||||||
hint: 'Endpoint was not found. Confirm OPENAI_BASE_URL includes /v1 for OpenAI-compatible local providers.',
|
requestUrl: options.url,
|
||||||
|
hint: isRemote
|
||||||
|
? `Endpoint at ${hostname} returned 404. Verify OPENAI_BASE_URL is correct and the requested model is supported by this provider.`
|
||||||
|
: 'Endpoint was not found. Confirm OPENAI_BASE_URL includes /v1 for OpenAI-compatible local providers.',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1935,7 +1935,9 @@ class OpenAIShimMessages {
|
|||||||
classifyOpenAIHttpFailure({
|
classifyOpenAIHttpFailure({
|
||||||
status,
|
status,
|
||||||
body: errorBody,
|
body: errorBody,
|
||||||
|
url: requestUrl,
|
||||||
})
|
})
|
||||||
|
const failureWithUrl = { ...failure, requestUrl: failure.requestUrl ?? requestUrl }
|
||||||
const redactedUrl = redactUrlForDiagnostics(requestUrl)
|
const redactedUrl = redactUrlForDiagnostics(requestUrl)
|
||||||
|
|
||||||
logForDebugging(
|
logForDebugging(
|
||||||
@@ -1948,7 +1950,7 @@ class OpenAIShimMessages {
|
|||||||
parsedBody,
|
parsedBody,
|
||||||
buildOpenAICompatibilityErrorMessage(
|
buildOpenAICompatibilityErrorMessage(
|
||||||
`OpenAI API error ${status}: ${errorBody}${rateHint}`,
|
`OpenAI API error ${status}: ${errorBody}${rateHint}`,
|
||||||
failure,
|
failureWithUrl,
|
||||||
),
|
),
|
||||||
responseHeaders,
|
responseHeaders,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user