mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-08 02:17:46 +08:00
feat: improve failover rules and request timeline
This commit is contained in:
@@ -19,45 +19,95 @@
|
||||
HTTP 200 但响应体匹配正则时,视为失败并触发转移
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
class="shrink-0"
|
||||
@click="addRule('success')"
|
||||
>
|
||||
<Plus class="w-4 h-4 mr-1" />
|
||||
添加
|
||||
</Button>
|
||||
<div class="flex items-center gap-1 shrink-0">
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="h-7 text-xs px-2"
|
||||
:title="successJsonMode ? '切回成功转移表单' : '切到成功转移 JSON'"
|
||||
@click="toggleSuccessJsonMode"
|
||||
>
|
||||
<Code2 class="w-3 h-3 mr-1" />
|
||||
{{ successJsonMode ? '表单' : 'JSON' }}
|
||||
</Button>
|
||||
<Button
|
||||
v-if="successJsonMode"
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="h-7 px-2 text-xs"
|
||||
title="格式化成功转移 JSON"
|
||||
@click="formatSuccessJsonDraft"
|
||||
>
|
||||
<AlignLeft class="w-3 h-3 mr-1" />
|
||||
格式化
|
||||
</Button>
|
||||
<Button
|
||||
v-if="!successJsonMode"
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="h-7 text-xs px-2"
|
||||
@click="addRule('success')"
|
||||
>
|
||||
<Plus class="w-3 h-3 mr-1" />
|
||||
添加
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="successPatterns.length === 0"
|
||||
class="text-xs text-muted-foreground px-3 py-4 border border-dashed rounded-lg text-center"
|
||||
v-if="successJsonMode"
|
||||
class="space-y-2"
|
||||
>
|
||||
暂无规则
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-for="(rule, index) in successPatterns"
|
||||
:key="'s-' + index"
|
||||
class="flex items-center gap-1"
|
||||
>
|
||||
<Input
|
||||
v-model="rule.pattern"
|
||||
placeholder="例如: relay:.*格式错误"
|
||||
size="sm"
|
||||
class="font-mono text-xs flex-1"
|
||||
<Textarea
|
||||
:model-value="successJsonDraft"
|
||||
class="min-h-[160px] font-mono text-xs leading-relaxed"
|
||||
spellcheck="false"
|
||||
placeholder="[{ "pattern": "relay:.*格式错误" }]"
|
||||
@update:model-value="updateSuccessJsonDraft"
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="shrink-0 h-8 w-8 p-0 text-muted-foreground hover:text-destructive"
|
||||
@click="removeRule('success', index)"
|
||||
<div
|
||||
v-if="successJsonError"
|
||||
class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive"
|
||||
>
|
||||
<Trash2 class="w-3.5 h-3.5" />
|
||||
</Button>
|
||||
{{ successJsonError }}
|
||||
</div>
|
||||
<p class="text-xs text-muted-foreground">
|
||||
仅管理成功转移规则;JSON 应为数组,<code class="bg-muted px-1 rounded">pattern</code> 必填。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<template v-else>
|
||||
<div
|
||||
v-if="successPatterns.length === 0"
|
||||
class="text-xs text-muted-foreground px-3 py-4 border border-dashed rounded-lg text-center"
|
||||
>
|
||||
暂无规则
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-for="(rule, index) in successPatterns"
|
||||
:key="'s-' + index"
|
||||
class="flex items-center gap-1"
|
||||
>
|
||||
<Input
|
||||
v-model="rule.pattern"
|
||||
placeholder="例如: relay:.*格式错误"
|
||||
size="sm"
|
||||
class="font-mono text-xs flex-1"
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="shrink-0 h-8 w-8 p-0 text-muted-foreground hover:text-destructive"
|
||||
@click="removeRule('success', index)"
|
||||
>
|
||||
<Trash2 class="w-3.5 h-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<!-- 错误终止规则 -->
|
||||
@@ -68,54 +118,104 @@
|
||||
错误终止规则
|
||||
</h3>
|
||||
<p class="text-xs text-muted-foreground mt-0.5">
|
||||
HTTP 非 200 且响应体匹配正则时,停止转移并直接返回错误。可选填状态码缩小匹配范围
|
||||
HTTP 非 200 且规则命中时,停止转移并直接返回错误。状态码不填则所有错误状态都尝试匹配正则
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
class="shrink-0"
|
||||
@click="addRule('error')"
|
||||
>
|
||||
<Plus class="w-4 h-4 mr-1" />
|
||||
添加
|
||||
</Button>
|
||||
<div class="flex items-center gap-1 shrink-0">
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="h-7 text-xs px-2"
|
||||
:title="errorJsonMode ? '切回错误终止表单' : '切到错误终止 JSON'"
|
||||
@click="toggleErrorJsonMode"
|
||||
>
|
||||
<Code2 class="w-3 h-3 mr-1" />
|
||||
{{ errorJsonMode ? '表单' : 'JSON' }}
|
||||
</Button>
|
||||
<Button
|
||||
v-if="errorJsonMode"
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="h-7 px-2 text-xs"
|
||||
title="格式化错误终止 JSON"
|
||||
@click="formatErrorJsonDraft"
|
||||
>
|
||||
<AlignLeft class="w-3 h-3 mr-1" />
|
||||
格式化
|
||||
</Button>
|
||||
<Button
|
||||
v-if="!errorJsonMode"
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="h-7 text-xs px-2"
|
||||
@click="addRule('error')"
|
||||
>
|
||||
<Plus class="w-3 h-3 mr-1" />
|
||||
添加
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="errorPatterns.length === 0"
|
||||
class="text-xs text-muted-foreground px-3 py-4 border border-dashed rounded-lg text-center"
|
||||
v-if="errorJsonMode"
|
||||
class="space-y-2"
|
||||
>
|
||||
暂无规则
|
||||
<Textarea
|
||||
:model-value="errorJsonDraft"
|
||||
class="min-h-[180px] font-mono text-xs leading-relaxed"
|
||||
spellcheck="false"
|
||||
placeholder="[{ "pattern": "content_policy_violation" }, { "status_codes": [429, 500, 503], "pattern": "" }]"
|
||||
@update:model-value="updateErrorJsonDraft"
|
||||
/>
|
||||
<div
|
||||
v-if="errorJsonError"
|
||||
class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive"
|
||||
>
|
||||
{{ errorJsonError }}
|
||||
</div>
|
||||
<p class="text-xs text-muted-foreground">
|
||||
仅管理错误终止规则;JSON 应为数组,<code class="bg-muted px-1 rounded">status_codes</code> 选填数组,不填则所有错误状态都尝试匹配 <code class="bg-muted px-1 rounded">pattern</code>;两者至少填一个。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-for="(rule, index) in errorPatterns"
|
||||
:key="'e-' + index"
|
||||
class="flex items-center gap-1"
|
||||
>
|
||||
<Input
|
||||
v-model="statusCodeInputs[index]"
|
||||
placeholder="状态码 (可选)"
|
||||
size="sm"
|
||||
class="font-mono text-xs w-28 shrink-0"
|
||||
/>
|
||||
<Input
|
||||
v-model="rule.pattern"
|
||||
placeholder="例如: content_policy_violation"
|
||||
size="sm"
|
||||
class="font-mono text-xs flex-1"
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="shrink-0 h-8 w-8 p-0 text-muted-foreground hover:text-destructive"
|
||||
@click="removeRule('error', index)"
|
||||
<template v-else>
|
||||
<div
|
||||
v-if="errorPatterns.length === 0"
|
||||
class="text-xs text-muted-foreground px-3 py-4 border border-dashed rounded-lg text-center"
|
||||
>
|
||||
<Trash2 class="w-3.5 h-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
暂无规则
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-for="(rule, index) in errorPatterns"
|
||||
:key="'e-' + index"
|
||||
class="flex items-center gap-1"
|
||||
>
|
||||
<Input
|
||||
v-model="statusCodeInputs[index]"
|
||||
placeholder="状态码 (选填,可多个)"
|
||||
size="sm"
|
||||
class="font-mono text-xs w-40 shrink-0"
|
||||
/>
|
||||
<Input
|
||||
v-model="rule.pattern"
|
||||
placeholder="正则内容 (选填)"
|
||||
size="sm"
|
||||
class="font-mono text-xs flex-1"
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="shrink-0 h-8 w-8 p-0 text-muted-foreground hover:text-destructive"
|
||||
@click="removeRule('error', index)"
|
||||
>
|
||||
<Trash2 class="w-3.5 h-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -143,12 +243,13 @@ import {
|
||||
Dialog,
|
||||
Button,
|
||||
Input,
|
||||
Textarea,
|
||||
} from '@/components/ui'
|
||||
import { GitBranch, Plus, Trash2 } from 'lucide-vue-next'
|
||||
import { AlignLeft, Code2, GitBranch, Plus, Trash2 } from 'lucide-vue-next'
|
||||
import { useToast } from '@/composables/useToast'
|
||||
import { updateProvider, type ProviderWithEndpointsSummary } from '@/api/endpoints'
|
||||
import { parseApiError } from '@/utils/errorParser'
|
||||
import type { FailoverRuleItem } from '@/api/endpoints/types'
|
||||
import type { FailoverRuleItem, FailoverRulesConfig } from '@/api/endpoints/types'
|
||||
|
||||
const props = defineProps<{
|
||||
open: boolean
|
||||
@@ -166,15 +267,105 @@ const saving = ref(false)
|
||||
const successPatterns = ref<FailoverRuleItem[]>([])
|
||||
const errorPatterns = ref<FailoverRuleItem[]>([])
|
||||
const statusCodeInputs = ref<string[]>([])
|
||||
const successJsonMode = ref(false)
|
||||
const successJsonDraft = ref('')
|
||||
const successJsonError = ref<string | null>(null)
|
||||
const successJsonDirty = ref(false)
|
||||
const errorJsonMode = ref(false)
|
||||
const errorJsonDraft = ref('')
|
||||
const errorJsonError = ref<string | null>(null)
|
||||
const errorJsonDirty = ref(false)
|
||||
|
||||
const TOP_LEVEL_STOP_STATUS_CODE_KEYS = [
|
||||
'stop_status_codes',
|
||||
'stop_on_status_codes',
|
||||
'early_stop_status_codes',
|
||||
'non_retryable_status_codes',
|
||||
] as const
|
||||
|
||||
const MANAGED_FAILOVER_RULE_KEYS = [
|
||||
'success_failover_patterns',
|
||||
'error_stop_patterns',
|
||||
...TOP_LEVEL_STOP_STATUS_CODE_KEYS,
|
||||
] as const
|
||||
|
||||
function isJsonObject(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function uniqueStatusCodes(codes: number[]): number[] {
|
||||
return codes.filter((code, index, values) =>
|
||||
Number.isInteger(code) && code >= 100 && code <= 599 && values.indexOf(code) === index
|
||||
)
|
||||
}
|
||||
|
||||
function collectTopLevelStopStatusCodes(rules: ProviderWithEndpointsSummary['failover_rules']): number[] {
|
||||
if (!rules) return []
|
||||
return uniqueStatusCodes(TOP_LEVEL_STOP_STATUS_CODE_KEYS.flatMap(key => rules[key] || []))
|
||||
}
|
||||
|
||||
function hasPersistableFailoverValue(value: unknown): boolean {
|
||||
if (value === null || value === undefined) return false
|
||||
if (Array.isArray(value)) return value.length > 0
|
||||
if (typeof value === 'string') return value.trim().length > 0
|
||||
if (isJsonObject(value)) return Object.values(value).some(hasPersistableFailoverValue)
|
||||
return true
|
||||
}
|
||||
|
||||
function buildPreservedFailoverRules(
|
||||
rules: ProviderWithEndpointsSummary['failover_rules'],
|
||||
): Record<string, unknown> {
|
||||
if (!isJsonObject(rules)) return {}
|
||||
const preserved: Record<string, unknown> = { ...rules }
|
||||
for (const key of MANAGED_FAILOVER_RULE_KEYS) {
|
||||
delete preserved[key]
|
||||
}
|
||||
return preserved
|
||||
}
|
||||
|
||||
function buildNextFailoverRules(
|
||||
filteredSuccess: FailoverRuleItem[],
|
||||
filteredError: FailoverRuleItem[],
|
||||
): FailoverRulesConfig | null {
|
||||
const nextRules = buildPreservedFailoverRules(props.provider?.failover_rules)
|
||||
if (filteredSuccess.length > 0) {
|
||||
nextRules.success_failover_patterns = filteredSuccess
|
||||
}
|
||||
if (filteredError.length > 0) {
|
||||
nextRules.error_stop_patterns = filteredError
|
||||
}
|
||||
|
||||
return Object.values(nextRules).some(hasPersistableFailoverValue)
|
||||
? nextRules as FailoverRulesConfig
|
||||
: null
|
||||
}
|
||||
|
||||
watch(() => [props.open, props.provider], () => {
|
||||
if (props.open && props.provider) {
|
||||
const rules = props.provider.failover_rules
|
||||
successPatterns.value = (rules?.success_failover_patterns || []).map(r => ({ ...r }))
|
||||
errorPatterns.value = (rules?.error_stop_patterns || []).map(r => ({ ...r }))
|
||||
successPatterns.value = (rules?.success_failover_patterns || []).map(r => ({
|
||||
...r,
|
||||
pattern: r.pattern || '',
|
||||
}))
|
||||
errorPatterns.value = (rules?.error_stop_patterns || []).map(r => ({
|
||||
...r,
|
||||
pattern: r.pattern || '',
|
||||
}))
|
||||
const topLevelStopStatusCodes = collectTopLevelStopStatusCodes(rules)
|
||||
if (topLevelStopStatusCodes.length > 0) {
|
||||
errorPatterns.value.push({
|
||||
pattern: '',
|
||||
description: '',
|
||||
status_codes: topLevelStopStatusCodes,
|
||||
})
|
||||
}
|
||||
statusCodeInputs.value = errorPatterns.value.map(r =>
|
||||
r.status_codes?.length ? r.status_codes.join(',') : ''
|
||||
)
|
||||
successJsonMode.value = false
|
||||
errorJsonMode.value = false
|
||||
refreshSuccessJsonDraft()
|
||||
refreshErrorJsonDraft()
|
||||
}
|
||||
}, { immediate: true })
|
||||
|
||||
@@ -201,9 +392,16 @@ function handleClose() {
|
||||
emit('update:open', false)
|
||||
}
|
||||
|
||||
function parseStatusCodes(input: string): { valid: true; codes?: number[] } | { valid: false; reason: string } {
|
||||
function parseStatusCodes(
|
||||
input: string,
|
||||
required = false,
|
||||
): { valid: true; codes: number[] } | { valid: false; reason: string } {
|
||||
const trimmed = input.trim()
|
||||
if (!trimmed) return { valid: true }
|
||||
if (!trimmed) {
|
||||
return required
|
||||
? { valid: false, reason: '状态码不能为空' }
|
||||
: { valid: true, codes: [] }
|
||||
}
|
||||
const parts = trimmed.split(/[,\s]+/)
|
||||
const codes: number[] = []
|
||||
for (const part of parts) {
|
||||
@@ -213,11 +411,21 @@ function parseStatusCodes(input: string): { valid: true; codes?: number[] } | {
|
||||
if (n < 100 || n > 599) return { valid: false, reason: `${n} 不在 100-599 范围内` }
|
||||
codes.push(n)
|
||||
}
|
||||
return { valid: true, codes: codes.length > 0 ? codes : undefined }
|
||||
const uniqueCodes = Array.from(new Set(codes))
|
||||
return uniqueCodes.length > 0
|
||||
? { valid: true, codes: uniqueCodes }
|
||||
: required
|
||||
? { valid: false, reason: '状态码不能为空' }
|
||||
: { valid: true, codes: [] }
|
||||
}
|
||||
|
||||
function validatePattern(pattern: string): string | null {
|
||||
if (!pattern.trim()) return '正则表达式不能为空'
|
||||
return validateOptionalPattern(pattern)
|
||||
}
|
||||
|
||||
function validateOptionalPattern(pattern: string): string | null {
|
||||
if (!pattern.trim()) return null
|
||||
try {
|
||||
new RegExp(pattern)
|
||||
return null
|
||||
@@ -226,12 +434,301 @@ function validatePattern(pattern: string): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
function buildSuccessJsonRulesFromForm(): FailoverRuleItem[] {
|
||||
return successPatterns.value
|
||||
.map(rule => ({
|
||||
...rule,
|
||||
pattern: rule.pattern.trim(),
|
||||
status_codes: rule.status_codes?.length ? uniqueStatusCodes(rule.status_codes) : undefined,
|
||||
}))
|
||||
.filter(rule => rule.pattern)
|
||||
}
|
||||
|
||||
function buildErrorJsonRulesFromForm(): FailoverRuleItem[] {
|
||||
return errorPatterns.value
|
||||
.map((rule, index) => {
|
||||
const parsed = parseStatusCodes(statusCodeInputs.value[index] || '')
|
||||
const statusCodes = parsed.valid
|
||||
? parsed.codes
|
||||
: uniqueStatusCodes(rule.status_codes || [])
|
||||
return {
|
||||
...rule,
|
||||
pattern: rule.pattern.trim(),
|
||||
status_codes: statusCodes.length > 0 ? statusCodes : undefined,
|
||||
}
|
||||
})
|
||||
.filter(rule => rule.pattern || (rule.status_codes?.length || 0) > 0)
|
||||
}
|
||||
|
||||
function stringifyRuleArray(rules: FailoverRuleItem[]): string {
|
||||
return JSON.stringify(rules, null, 2)
|
||||
}
|
||||
|
||||
function refreshSuccessJsonDraft() {
|
||||
successJsonDraft.value = stringifyRuleArray(buildSuccessJsonRulesFromForm())
|
||||
successJsonError.value = null
|
||||
successJsonDirty.value = false
|
||||
}
|
||||
|
||||
function refreshErrorJsonDraft() {
|
||||
errorJsonDraft.value = stringifyRuleArray(buildErrorJsonRulesFromForm())
|
||||
errorJsonError.value = null
|
||||
errorJsonDirty.value = false
|
||||
}
|
||||
|
||||
function updateSuccessJsonDraft(value: string) {
|
||||
successJsonDraft.value = value
|
||||
successJsonDirty.value = true
|
||||
successJsonError.value = null
|
||||
}
|
||||
|
||||
function updateErrorJsonDraft(value: string) {
|
||||
errorJsonDraft.value = value
|
||||
errorJsonDirty.value = true
|
||||
errorJsonError.value = null
|
||||
}
|
||||
|
||||
function readJsonRuleArray(
|
||||
parsed: unknown,
|
||||
key: 'success_failover_patterns' | 'error_stop_patterns',
|
||||
): { root: Record<string, unknown>; value: unknown[]; error: string | null } {
|
||||
if (Array.isArray(parsed)) return { root: {}, value: parsed, error: null }
|
||||
if (!isJsonObject(parsed)) return { root: {}, value: [], error: '规则 JSON 必须是数组或对象' }
|
||||
const root = isJsonObject(parsed.failover_rules) ? parsed.failover_rules : parsed
|
||||
const raw = root[key]
|
||||
if (raw === undefined || raw === null) return { root, value: [], error: null }
|
||||
if (!Array.isArray(raw)) return { root, value: [], error: `${key} 必须是数组或 null` }
|
||||
return { root, value: raw, error: null }
|
||||
}
|
||||
|
||||
function parseJsonStatusCodes(
|
||||
value: unknown,
|
||||
label: string,
|
||||
required: boolean,
|
||||
): { codes: number[]; error: string | null } {
|
||||
if (value === undefined || value === null) {
|
||||
return required
|
||||
? { codes: [], error: `${label}status_codes 必填` }
|
||||
: { codes: [], error: null }
|
||||
}
|
||||
if (!Array.isArray(value)) return { codes: [], error: `${label}status_codes 必须是数组` }
|
||||
const codes: number[] = []
|
||||
for (const item of value) {
|
||||
if (!Number.isInteger(item)) {
|
||||
return { codes: [], error: `${label}status_codes 只能包含整数` }
|
||||
}
|
||||
const code = item as number
|
||||
if (code < 100 || code > 599) {
|
||||
return { codes: [], error: `${label}status_codes 只能填写 100-599` }
|
||||
}
|
||||
codes.push(code)
|
||||
}
|
||||
const uniqueCodes = uniqueStatusCodes(codes)
|
||||
if (required && uniqueCodes.length === 0) {
|
||||
return { codes: [], error: `${label}status_codes 必填` }
|
||||
}
|
||||
return { codes: uniqueCodes, error: null }
|
||||
}
|
||||
|
||||
function parseJsonSuccessRule(rule: unknown, index: number): { rule: FailoverRuleItem | null; error: string | null } {
|
||||
const label = `成功转移 JSON 第 ${index + 1} 条:`
|
||||
if (!isJsonObject(rule)) return { rule: null, error: `${label}必须是对象` }
|
||||
if (typeof rule.pattern !== 'string' || !rule.pattern.trim()) {
|
||||
return { rule: null, error: `${label}pattern 必填` }
|
||||
}
|
||||
const pattern = rule.pattern.trim()
|
||||
const patternError = validatePattern(pattern)
|
||||
if (patternError) return { rule: null, error: `${label}${patternError}` }
|
||||
const status = parseJsonStatusCodes(rule.status_codes, label, false)
|
||||
if (status.error) return { rule: null, error: status.error }
|
||||
|
||||
return {
|
||||
rule: {
|
||||
pattern,
|
||||
description: typeof rule.description === 'string' ? rule.description : undefined,
|
||||
status_codes: status.codes.length > 0 ? status.codes : undefined,
|
||||
},
|
||||
error: null,
|
||||
}
|
||||
}
|
||||
|
||||
function parseJsonErrorRule(rule: unknown, index: number): { rule: FailoverRuleItem | null; error: string | null } {
|
||||
const label = `错误终止 JSON 第 ${index + 1} 条:`
|
||||
if (!isJsonObject(rule)) return { rule: null, error: `${label}必须是对象` }
|
||||
const status = parseJsonStatusCodes(rule.status_codes, label, false)
|
||||
if (status.error) return { rule: null, error: status.error }
|
||||
if (rule.pattern !== undefined && rule.pattern !== null && typeof rule.pattern !== 'string') {
|
||||
return { rule: null, error: `${label}pattern 必须是字符串` }
|
||||
}
|
||||
const pattern = typeof rule.pattern === 'string' ? rule.pattern.trim() : ''
|
||||
const patternError = validateOptionalPattern(pattern)
|
||||
if (patternError) return { rule: null, error: `${label}${patternError}` }
|
||||
if (status.codes.length === 0 && !pattern) {
|
||||
return { rule: null, error: `${label}status_codes 和 pattern 至少填写一个` }
|
||||
}
|
||||
|
||||
return {
|
||||
rule: {
|
||||
pattern,
|
||||
description: typeof rule.description === 'string' ? rule.description : undefined,
|
||||
status_codes: status.codes.length > 0 ? status.codes : undefined,
|
||||
},
|
||||
error: null,
|
||||
}
|
||||
}
|
||||
|
||||
function parseSuccessRulesJsonDraft(draft: string): { value: FailoverRuleItem[] | null; error: string | null } {
|
||||
const raw = draft.trim()
|
||||
if (!raw) return { value: [], error: null }
|
||||
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(raw)
|
||||
} catch (error: unknown) {
|
||||
return { value: null, error: error instanceof Error ? error.message : 'JSON 格式无效' }
|
||||
}
|
||||
|
||||
const rules = readJsonRuleArray(parsed, 'success_failover_patterns')
|
||||
if (rules.error) return { value: null, error: rules.error }
|
||||
|
||||
const normalized: FailoverRuleItem[] = []
|
||||
for (let i = 0; i < rules.value.length; i++) {
|
||||
const parsedRule = parseJsonSuccessRule(rules.value[i], i)
|
||||
if (parsedRule.error || !parsedRule.rule) return { value: null, error: parsedRule.error }
|
||||
normalized.push(parsedRule.rule)
|
||||
}
|
||||
return { value: normalized, error: null }
|
||||
}
|
||||
|
||||
function parseErrorRulesJsonDraft(draft: string): { value: FailoverRuleItem[] | null; error: string | null } {
|
||||
const raw = draft.trim()
|
||||
if (!raw) return { value: [], error: null }
|
||||
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(raw)
|
||||
} catch (error: unknown) {
|
||||
return { value: null, error: error instanceof Error ? error.message : 'JSON 格式无效' }
|
||||
}
|
||||
|
||||
const rules = readJsonRuleArray(parsed, 'error_stop_patterns')
|
||||
if (rules.error) return { value: null, error: rules.error }
|
||||
|
||||
const normalized: FailoverRuleItem[] = []
|
||||
for (let i = 0; i < rules.value.length; i++) {
|
||||
const parsedRule = parseJsonErrorRule(rules.value[i], i)
|
||||
if (parsedRule.error || !parsedRule.rule) return { value: null, error: parsedRule.error }
|
||||
normalized.push(parsedRule.rule)
|
||||
}
|
||||
|
||||
for (const key of TOP_LEVEL_STOP_STATUS_CODE_KEYS) {
|
||||
const status = parseJsonStatusCodes(rules.root[key], `${key}:`, false)
|
||||
if (status.error) return { value: null, error: status.error }
|
||||
if (status.codes.length > 0) {
|
||||
normalized.push({
|
||||
pattern: '',
|
||||
description: '',
|
||||
status_codes: status.codes,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return { value: normalized, error: null }
|
||||
}
|
||||
|
||||
function applySuccessJsonDraft(options: { notify?: boolean; notifyError?: boolean } = {}): boolean {
|
||||
const notifyError = options.notifyError !== false
|
||||
const parsed = parseSuccessRulesJsonDraft(successJsonDraft.value)
|
||||
if (!parsed.value) {
|
||||
successJsonError.value = parsed.error
|
||||
if (notifyError) showError(parsed.error || '成功转移规则 JSON 无效', '验证失败')
|
||||
return false
|
||||
}
|
||||
|
||||
successPatterns.value = parsed.value.map(rule => ({ ...rule }))
|
||||
successJsonDraft.value = stringifyRuleArray(parsed.value)
|
||||
successJsonError.value = null
|
||||
successJsonDirty.value = false
|
||||
if (options.notify !== false) success('成功转移 JSON 已应用')
|
||||
return true
|
||||
}
|
||||
|
||||
function applyErrorJsonDraft(options: { notify?: boolean; notifyError?: boolean } = {}): boolean {
|
||||
const notifyError = options.notifyError !== false
|
||||
const parsed = parseErrorRulesJsonDraft(errorJsonDraft.value)
|
||||
if (!parsed.value) {
|
||||
errorJsonError.value = parsed.error
|
||||
if (notifyError) showError(parsed.error || '错误终止规则 JSON 无效', '验证失败')
|
||||
return false
|
||||
}
|
||||
|
||||
errorPatterns.value = parsed.value.map(rule => ({ ...rule }))
|
||||
statusCodeInputs.value = errorPatterns.value.map(rule => rule.status_codes?.join(',') || '')
|
||||
errorJsonDraft.value = stringifyRuleArray(parsed.value)
|
||||
errorJsonError.value = null
|
||||
errorJsonDirty.value = false
|
||||
if (options.notify !== false) success('错误终止 JSON 已应用')
|
||||
return true
|
||||
}
|
||||
|
||||
function toggleSuccessJsonMode() {
|
||||
if (successJsonMode.value) {
|
||||
if (successJsonDirty.value && !applySuccessJsonDraft({ notify: false })) return
|
||||
successJsonMode.value = false
|
||||
return
|
||||
}
|
||||
refreshSuccessJsonDraft()
|
||||
successJsonMode.value = true
|
||||
}
|
||||
|
||||
function toggleErrorJsonMode() {
|
||||
if (errorJsonMode.value) {
|
||||
if (errorJsonDirty.value && !applyErrorJsonDraft({ notify: false })) return
|
||||
errorJsonMode.value = false
|
||||
return
|
||||
}
|
||||
refreshErrorJsonDraft()
|
||||
errorJsonMode.value = true
|
||||
}
|
||||
|
||||
function formatSuccessJsonDraft() {
|
||||
const currentDraft = successJsonDraft.value
|
||||
const parsed = parseSuccessRulesJsonDraft(currentDraft)
|
||||
if (!parsed.value) {
|
||||
successJsonError.value = parsed.error
|
||||
return
|
||||
}
|
||||
const formattedDraft = stringifyRuleArray(parsed.value)
|
||||
successJsonDraft.value = formattedDraft
|
||||
successJsonError.value = null
|
||||
if (formattedDraft !== currentDraft) {
|
||||
successJsonDirty.value = true
|
||||
}
|
||||
}
|
||||
|
||||
function formatErrorJsonDraft() {
|
||||
const currentDraft = errorJsonDraft.value
|
||||
const parsed = parseErrorRulesJsonDraft(currentDraft)
|
||||
if (!parsed.value) {
|
||||
errorJsonError.value = parsed.error
|
||||
return
|
||||
}
|
||||
const formattedDraft = stringifyRuleArray(parsed.value)
|
||||
errorJsonDraft.value = formattedDraft
|
||||
errorJsonError.value = null
|
||||
if (formattedDraft !== currentDraft) {
|
||||
errorJsonDirty.value = true
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSave() {
|
||||
if (!props.provider) return
|
||||
|
||||
if (successJsonMode.value && !applySuccessJsonDraft({ notify: false })) return
|
||||
if (errorJsonMode.value && !applyErrorJsonDraft({ notify: false })) return
|
||||
|
||||
// Validate patterns
|
||||
const allPatterns = [...successPatterns.value, ...errorPatterns.value]
|
||||
for (const rule of allPatterns) {
|
||||
for (const rule of successPatterns.value) {
|
||||
const err = validatePattern(rule.pattern)
|
||||
if (err) {
|
||||
showError(err, '验证失败')
|
||||
@@ -247,23 +744,39 @@ async function handleSave() {
|
||||
showError(`状态码格式错误: ${result.reason},请输入 100-599 之间的整数,多个用逗号分隔`, '验证失败')
|
||||
return
|
||||
}
|
||||
errorPatterns.value[i].status_codes = result.codes
|
||||
const patternErr = validateOptionalPattern(errorPatterns.value[i].pattern)
|
||||
if (patternErr) {
|
||||
showError(patternErr, '验证失败')
|
||||
return
|
||||
}
|
||||
const pattern = errorPatterns.value[i].pattern.trim()
|
||||
if (result.codes.length === 0 && !pattern) {
|
||||
showError(`第 ${i + 1} 条错误终止规则:状态码和正则内容至少填写一个`, '验证失败')
|
||||
return
|
||||
}
|
||||
errorPatterns.value[i].status_codes = result.codes.length > 0 ? result.codes : undefined
|
||||
}
|
||||
|
||||
saving.value = true
|
||||
try {
|
||||
const filteredSuccess = successPatterns.value.filter(r => r.pattern.trim())
|
||||
const filteredError = errorPatterns.value.filter(r => r.pattern.trim())
|
||||
const filteredSuccess = successPatterns.value
|
||||
.map(r => ({ ...r, pattern: r.pattern.trim() }))
|
||||
.filter(r => r.pattern)
|
||||
const filteredError = errorPatterns.value
|
||||
.map(r => {
|
||||
const statusCodes = r.status_codes?.length ? r.status_codes : undefined
|
||||
return {
|
||||
...r,
|
||||
pattern: r.pattern.trim(),
|
||||
status_codes: statusCodes,
|
||||
}
|
||||
})
|
||||
.filter(r => r.pattern || (r.status_codes?.length || 0) > 0)
|
||||
|
||||
const hasRules = filteredSuccess.length > 0 || filteredError.length > 0
|
||||
const nextFailoverRules = buildNextFailoverRules(filteredSuccess, filteredError)
|
||||
|
||||
await updateProvider(props.provider.id, {
|
||||
failover_rules: hasRules
|
||||
? {
|
||||
success_failover_patterns: filteredSuccess,
|
||||
error_stop_patterns: filteredError,
|
||||
}
|
||||
: null,
|
||||
failover_rules: nextFailoverRules,
|
||||
})
|
||||
|
||||
success('故障转移规则已保存')
|
||||
|
||||
@@ -1627,11 +1627,23 @@ const antigravityQuotaDialogKey = ref<EndpointAPIKey | null>(null)
|
||||
|
||||
// 故障转移规则
|
||||
const failoverRulesDialogOpen = ref(false)
|
||||
const FAILOVER_RULE_ARRAY_KEYS = [
|
||||
'success_failover_patterns',
|
||||
'error_stop_patterns',
|
||||
'stop_status_codes',
|
||||
'stop_on_status_codes',
|
||||
'early_stop_status_codes',
|
||||
'non_retryable_status_codes',
|
||||
'continue_on_status_codes',
|
||||
'retryable_status_codes',
|
||||
'retry_on_status_codes',
|
||||
'continue_status_codes',
|
||||
] as const
|
||||
const hasFailoverRules = computed(() => {
|
||||
const rules = provider.value?.failover_rules
|
||||
if (!rules) return false
|
||||
return (rules.success_failover_patterns?.length || 0) > 0
|
||||
|| (rules.error_stop_patterns?.length || 0) > 0
|
||||
return FAILOVER_RULE_ARRAY_KEYS.some(key => (rules[key]?.length || 0) > 0)
|
||||
|| typeof rules.max_retries === 'number'
|
||||
})
|
||||
|
||||
// Provider 级别代理配置状态
|
||||
|
||||
@@ -1401,9 +1401,8 @@ const currentAttemptFailureDiagnostic = computed<{
|
||||
const attempt = currentAttempt.value
|
||||
if (!attempt) return null
|
||||
const extra = extractObject(attempt.extra_data)
|
||||
const failureDiagnostic = extractObject(extra?.failure_diagnostic)
|
||||
const safeToShow = failureDiagnostic?.safe_to_show !== false
|
||||
const error = failureDiagnostic && safeToShow
|
||||
const failureDiagnostic = extractVisibleFailureDiagnostic(extra)
|
||||
const error = failureDiagnostic
|
||||
? failureDiagnostic
|
||||
: extractObject(extra?.request_body_build_error)
|
||||
const path = typeof error?.path === 'string' && error.path.trim()
|
||||
@@ -1415,19 +1414,205 @@ const currentAttemptFailureDiagnostic = computed<{
|
||||
if (!path && !message) return null
|
||||
return {
|
||||
path: path || '$',
|
||||
message: message || '请求体转换失败',
|
||||
message: formatAttemptErrorMessage(message) || message || '请求体转换失败',
|
||||
}
|
||||
})
|
||||
|
||||
const isGenericExecutionRuntimeStatusMessage = (message: string): boolean =>
|
||||
/execution runtime (stream )?returned non-success status \d+/i.test(message)
|
||||
|
||||
const isLocalSyncFinalizeDiagnostic = (message: string): boolean =>
|
||||
/local sync attempt failed before terminal finalization/i.test(message)
|
||||
|| /unsupported provider stream (event|finish reason)/i.test(message)
|
||||
|
||||
const isActionableDiagnosticMessage = (message: string): boolean =>
|
||||
isLocalSyncFinalizeDiagnostic(message) || isConversionDiagnosticMessage(message)
|
||||
|
||||
const extractVisibleFailureDiagnostic = (
|
||||
extra: Record<string, unknown> | null | undefined,
|
||||
): Record<string, unknown> | null => {
|
||||
const failureDiagnostic = extractObject(extra?.failure_diagnostic)
|
||||
if (!failureDiagnostic || failureDiagnostic.safe_to_show === false) return null
|
||||
return failureDiagnostic
|
||||
}
|
||||
|
||||
const extractVisibleDiagnosticObjects = (
|
||||
extra: Record<string, unknown> | null | undefined,
|
||||
): Array<Record<string, unknown>> => [
|
||||
extractVisibleFailureDiagnostic(extra),
|
||||
extractObject(extra?.request_conversion_error),
|
||||
extractObject(extra?.request_body_build_error),
|
||||
].filter((value): value is Record<string, unknown> => Boolean(value))
|
||||
|
||||
const extractVisibleDiagnosticMessage = (
|
||||
extra: Record<string, unknown> | null | undefined,
|
||||
): string => {
|
||||
for (const diagnostic of extractVisibleDiagnosticObjects(extra)) {
|
||||
const message = readStringField(diagnostic, 'message')
|
||||
if (message) return message
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
const chooseAttemptRawErrorMessage = (
|
||||
flowMessage: string,
|
||||
fallbackMessage: string,
|
||||
diagnosticMessage: string,
|
||||
): string => {
|
||||
const flow = flowMessage.trim()
|
||||
const fallback = fallbackMessage.trim()
|
||||
const diagnostic = diagnosticMessage.trim()
|
||||
|
||||
if (flow && fallback && !isActionableDiagnosticMessage(flow) && isActionableDiagnosticMessage(fallback)) {
|
||||
return fallback
|
||||
}
|
||||
if (flow && diagnostic && isGenericExecutionRuntimeStatusMessage(flow) && isActionableDiagnosticMessage(diagnostic)) {
|
||||
return diagnostic
|
||||
}
|
||||
if (fallback && diagnostic && isGenericExecutionRuntimeStatusMessage(fallback) && isActionableDiagnosticMessage(diagnostic)) {
|
||||
return diagnostic
|
||||
}
|
||||
|
||||
return flow || fallback || diagnostic
|
||||
}
|
||||
|
||||
const decodeRustDebugString = (value: string): string => {
|
||||
try {
|
||||
return JSON.parse(`"${value}"`)
|
||||
} catch {
|
||||
return value.replace(/\\"/g, '"')
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeDiagnosticFieldPath = (field: string): string => {
|
||||
const trimmed = field.trim()
|
||||
if (!trimmed || trimmed === '$') return '$'
|
||||
if (trimmed.startsWith('$')) return trimmed
|
||||
if (trimmed.startsWith('[')) return `$${trimmed}`
|
||||
return `$.${trimmed}`
|
||||
}
|
||||
|
||||
const formatConversionPair = (source: string, target: string): string =>
|
||||
`${formatApiFormat(source.trim())} → ${formatApiFormat(target.trim())}`
|
||||
|
||||
const extractFieldDetail = (message: string): string => {
|
||||
const fieldMatch = message.match(/field\s+([^;=]+?)\s*=\s*(\"(?:\\.|[^"\\])*\"|[^;]+)/i)
|
||||
const unsupportedFieldMatch = message.match(/field\s+([^;]+?)\s+is unsupported/i)
|
||||
if (fieldMatch?.[1]) {
|
||||
return `字段 ${normalizeDiagnosticFieldPath(fieldMatch[1])} = ${fieldMatch[2].trim()}`
|
||||
}
|
||||
if (unsupportedFieldMatch?.[1]) {
|
||||
return `字段 ${normalizeDiagnosticFieldPath(unsupportedFieldMatch[1])} 不支持`
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
const formatUnsupportedStreamEventMessage = (message: string): string => {
|
||||
const detail = extractFieldDetail(message)
|
||||
const fieldDetail = detail ? `(${detail})` : ''
|
||||
return `流式格式转换失败:上游返回了当前不支持的 stream event${fieldDetail},无法无损转换到客户端请求格式`
|
||||
}
|
||||
|
||||
const formatUnsupportedFinishReasonMessage = (message: string): string => {
|
||||
const fieldDetail = extractFieldDetail(message)
|
||||
const legacyMatch = message.match(/unsupported provider stream finish reason\s+(.+?)\s+cannot be converted losslessly/i)
|
||||
const detail = fieldDetail || (legacyMatch?.[1]
|
||||
? `字段 $.finish_reason = ${legacyMatch[1].trim()}`
|
||||
: '')
|
||||
return `流式格式转换失败:上游返回了当前不支持的 finish reason${detail ? `(${detail})` : ''},无法无损转换到客户端请求格式`
|
||||
}
|
||||
|
||||
const formatKnownConversionErrorMessage = (message: string): string => {
|
||||
const lossy = message.match(/^lossy conversion blocked from\s+(\S+)\s+to\s+(\S+)\s+at\s+([^:]+):\s*(.+)$/i)
|
||||
if (lossy) {
|
||||
return `格式转换失败:${formatConversionPair(lossy[1], lossy[2])} 在字段 ${normalizeDiagnosticFieldPath(lossy[3])} 会丢失信息:${lossy[4].trim()}`
|
||||
}
|
||||
|
||||
const unaudited = message.match(/^unaudited field\s+(.+?)\s+in\s+(.+?)\s+cannot be converted to\s+([^:]+):\s*(.+)$/i)
|
||||
if (unaudited) {
|
||||
return `格式转换失败:${formatConversionPair(unaudited[2], unaudited[3])} 的字段 ${normalizeDiagnosticFieldPath(unaudited[1])} 尚未审计,不能安全转换:${unaudited[4].trim()}`
|
||||
}
|
||||
|
||||
const unsupportedField = message.match(/^unsupported field\s+(.+?)\s+in\s+([^:]+(?::[^:]+)?):\s*(.+)$/i)
|
||||
if (unsupportedField) {
|
||||
return `格式转换失败:${formatApiFormat(unsupportedField[2])} 不支持字段 ${normalizeDiagnosticFieldPath(unsupportedField[1])}:${unsupportedField[3].trim()}`
|
||||
}
|
||||
|
||||
const invalidEnum = message.match(/^invalid enum value\s+(.+?)\s+for\s+(.+)\.([^.\s]+)$/i)
|
||||
if (invalidEnum) {
|
||||
return `格式转换失败:${formatApiFormat(invalidEnum[2])} 字段 ${normalizeDiagnosticFieldPath(invalidEnum[3])} 的枚举值 ${invalidEnum[1].trim()} 无效`
|
||||
}
|
||||
|
||||
const invalidTarget = message.match(/^invalid target field\s+(.+?)\s+for\s+(.+?):\s*(.+)$/i)
|
||||
if (invalidTarget) {
|
||||
return `格式转换失败:目标格式 ${formatApiFormat(invalidTarget[2])} 字段 ${normalizeDiagnosticFieldPath(invalidTarget[1])} 无效:${invalidTarget[3].trim()}`
|
||||
}
|
||||
|
||||
const unsupportedFormat = message.match(/^unsupported AI format:\s*(.+)$/i)
|
||||
if (unsupportedFormat) {
|
||||
return `格式转换失败:不支持的 API 格式 ${unsupportedFormat[1].trim()}`
|
||||
}
|
||||
|
||||
const parseEmit = message.match(/^failed to\s+(parse|emit)\s+(.+?)\s+(request|response)$/i)
|
||||
if (parseEmit) {
|
||||
const action = parseEmit[1].toLowerCase() === 'parse' ? '解析' : '生成'
|
||||
const subject = parseEmit[3].toLowerCase() === 'request' ? '请求体' : '响应体'
|
||||
return `格式转换失败:无法${action} ${formatApiFormat(parseEmit[2])} ${subject}`
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
const isConversionDiagnosticMessage = (message: string): boolean => {
|
||||
const normalized = message.trim()
|
||||
if (!normalized) return false
|
||||
return /conversion|converted|convertible|cannot be converted|lossy conversion|unsupported field|unaudited field|invalid enum value|invalid target field|unsupported ai format|failed to (parse|emit) .+ (request|response)|unsupported provider stream (event|finish reason)|转换|无损|字段 .*不支持/i
|
||||
.test(normalized)
|
||||
}
|
||||
|
||||
const formatAttemptErrorMessage = (message: string, statusCode?: number): string => {
|
||||
const normalized = message.trim()
|
||||
if (!normalized) return ''
|
||||
if (/execution runtime (stream )?returned non-success status \d+/i.test(normalized)) {
|
||||
const directInternal = normalized.match(/^Internal\("((?:\\.|[^"\\])*)"\)$/i)
|
||||
if (directInternal?.[1]) {
|
||||
return formatAttemptErrorMessage(decodeRustDebugString(directInternal[1]), statusCode)
|
||||
}
|
||||
const localSyncInternal = normalized.match(/local sync attempt failed before terminal finalization:\s*Internal\("((?:\\.|[^"\\])*)"\)/i)
|
||||
if (localSyncInternal?.[1]) {
|
||||
return formatAttemptErrorMessage(decodeRustDebugString(localSyncInternal[1]), statusCode)
|
||||
}
|
||||
if (/unsupported provider stream event cannot be converted losslessly/i.test(normalized)) {
|
||||
return formatUnsupportedStreamEventMessage(normalized)
|
||||
}
|
||||
if (/unsupported provider stream finish reason/i.test(normalized)) {
|
||||
return formatUnsupportedFinishReasonMessage(normalized)
|
||||
}
|
||||
const conversionMessage = formatKnownConversionErrorMessage(normalized)
|
||||
if (conversionMessage) {
|
||||
return conversionMessage
|
||||
}
|
||||
if (isGenericExecutionRuntimeStatusMessage(normalized)) {
|
||||
return statusCode != null ? `上游返回非成功状态 ${statusCode}` : '上游返回非成功状态'
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
const shouldShowAttemptMessageWithUpstreamResponse = (
|
||||
rawMessage: string,
|
||||
upstreamResponse: Record<string, unknown> | null,
|
||||
): boolean => {
|
||||
if (!upstreamResponse) return true
|
||||
const normalized = rawMessage.trim()
|
||||
if (!normalized) return false
|
||||
if (isLocalSyncFinalizeDiagnostic(normalized)) return true
|
||||
if (isConversionDiagnosticMessage(normalized)) return true
|
||||
if (isGenericExecutionRuntimeStatusMessage(normalized)) return false
|
||||
|
||||
const hasBody = hasRenderableValue(upstreamResponse.body)
|
||||
const bodyState = (readStringField(upstreamResponse, 'body_state') ?? '').toLowerCase()
|
||||
return !hasBody && bodyState === 'disabled'
|
||||
}
|
||||
|
||||
const currentAttemptRequestError = computed<{
|
||||
message: string
|
||||
statusCode?: number
|
||||
@@ -1453,17 +1638,177 @@ const currentAttemptRequestError = computed<{
|
||||
const fallbackType = typeof attempt.error_type === 'string' && attempt.error_type.trim()
|
||||
? attempt.error_type.trim()
|
||||
: ''
|
||||
const message = formatAttemptErrorMessage(flowMessage || fallbackMessage, statusCode) || fallbackType
|
||||
const diagnosticMessage = extractVisibleDiagnosticMessage(extra)
|
||||
const rawMessage = chooseAttemptRawErrorMessage(flowMessage || '', fallbackMessage, diagnosticMessage)
|
||||
const message = formatAttemptErrorMessage(rawMessage, statusCode) || fallbackType
|
||||
const upstreamResponseDisplay = normalizeUpstreamResponseDisplay(extra?.upstream_response)
|
||||
if (!message && statusCode == null && !upstreamResponseDisplay) return null
|
||||
const visibleDiagnosticObjects = extractVisibleDiagnosticObjects(extra)
|
||||
const shouldAttachDiagnostic = Boolean(
|
||||
visibleDiagnosticObjects.length
|
||||
|| isLocalSyncFinalizeDiagnostic(rawMessage)
|
||||
|| isConversionDiagnosticMessage(rawMessage),
|
||||
)
|
||||
const diagnostic = shouldAttachDiagnostic
|
||||
? buildAttemptDiagnosticPayload(
|
||||
attempt,
|
||||
message || fallbackType || rawMessage || '未知失败',
|
||||
statusCode,
|
||||
upstreamResponseDisplay,
|
||||
rawMessage,
|
||||
)
|
||||
: null
|
||||
const upstreamResponseWithDiagnostic = diagnostic
|
||||
? { ...(upstreamResponseDisplay ?? {}), diagnostic }
|
||||
: upstreamResponseDisplay
|
||||
if (!message && statusCode == null && !upstreamResponseWithDiagnostic) return null
|
||||
const showMessage = shouldShowAttemptMessageWithUpstreamResponse(
|
||||
rawMessage || fallbackType,
|
||||
upstreamResponseDisplay,
|
||||
)
|
||||
|
||||
return {
|
||||
message: upstreamResponseDisplay ? '' : (message || '未知错误'),
|
||||
message: showMessage ? (message || '未知错误') : '',
|
||||
statusCode,
|
||||
upstreamResponse: upstreamResponseDisplay,
|
||||
upstreamResponse: upstreamResponseWithDiagnostic,
|
||||
}
|
||||
})
|
||||
|
||||
const diagnosticPathFromObject = (value: unknown): string => {
|
||||
const object = extractObject(value)
|
||||
if (!object) return ''
|
||||
return readStringField(object, 'path')
|
||||
|| readStringField(object, 'field_path')
|
||||
|| readStringField(object, 'fieldPath')
|
||||
|| readStringField(object, 'field')
|
||||
|| ''
|
||||
}
|
||||
|
||||
const diagnosticFieldPathFromMessage = (message: string): string => {
|
||||
const normalized = message.trim()
|
||||
if (!normalized) return ''
|
||||
const fieldMatch = normalized.match(/field\s+([^;=]+?)\s*(?:=|is unsupported|不支持)/i)
|
||||
if (fieldMatch?.[1]) return normalizeDiagnosticFieldPath(fieldMatch[1])
|
||||
const lossyMatch = normalized.match(/lossy conversion blocked from\s+\S+\s+to\s+\S+\s+at\s+([^:]+):/i)
|
||||
if (lossyMatch?.[1]) return normalizeDiagnosticFieldPath(lossyMatch[1])
|
||||
const invalidTargetMatch = normalized.match(/invalid target field\s+(.+?)\s+for\s+/i)
|
||||
if (invalidTargetMatch?.[1]) return normalizeDiagnosticFieldPath(invalidTargetMatch[1])
|
||||
const unsupportedFieldMatch = normalized.match(/unsupported field\s+(.+?)\s+in\s+/i)
|
||||
if (unsupportedFieldMatch?.[1]) return normalizeDiagnosticFieldPath(unsupportedFieldMatch[1])
|
||||
const invalidEnumMatch = normalized.match(/invalid enum value\s+.+?\s+for\s+.+\.([^.\s]+)$/i)
|
||||
if (invalidEnumMatch?.[1]) return normalizeDiagnosticFieldPath(invalidEnumMatch[1])
|
||||
return ''
|
||||
}
|
||||
|
||||
function resolveAttemptDiagnosticBreakpoint(attempt: CandidateRecord, rawMessageOverride = ''): string {
|
||||
const extra = extractObject(attempt.extra_data)
|
||||
const failureDiagnostic = extractVisibleFailureDiagnostic(extra)
|
||||
const requestConversionError = extractObject(extra?.request_conversion_error)
|
||||
const requestBodyBuildError = extractObject(extra?.request_body_build_error)
|
||||
const errorFlow = extractObject(extra?.error_flow)
|
||||
const rawMessage = [
|
||||
rawMessageOverride,
|
||||
readStringField(errorFlow ?? {}, 'message') ?? '',
|
||||
readStringField(failureDiagnostic ?? {}, 'message') ?? '',
|
||||
readStringField(requestConversionError ?? {}, 'message') ?? '',
|
||||
readStringField(requestBodyBuildError ?? {}, 'message') ?? '',
|
||||
typeof attempt.error_message === 'string' ? attempt.error_message : '',
|
||||
].find(item => item.trim()) ?? ''
|
||||
|
||||
return diagnosticPathFromObject(failureDiagnostic)
|
||||
|| diagnosticPathFromObject(requestConversionError)
|
||||
|| diagnosticPathFromObject(requestBodyBuildError)
|
||||
|| diagnosticFieldPathFromMessage(rawMessage)
|
||||
|| '$'
|
||||
}
|
||||
|
||||
function buildAttemptDiagnosticPayload(
|
||||
attempt: CandidateRecord,
|
||||
summaryInput: string,
|
||||
statusCode: number | undefined,
|
||||
upstreamResponseDisplay: Record<string, unknown> | null,
|
||||
rawMessageForBreakpoint = '',
|
||||
): Record<string, unknown> | null {
|
||||
const extra = extractObject(attempt.extra_data)
|
||||
const rawFailureDiagnostic = extractVisibleFailureDiagnostic(extra)
|
||||
const rawRequestConversionError = extractObject(extra?.request_conversion_error)
|
||||
const rawRequestBodyBuildError = extractObject(extra?.request_body_build_error)
|
||||
const hasDiagnostic = Boolean(
|
||||
summaryInput
|
||||
|| rawFailureDiagnostic
|
||||
|| rawRequestConversionError
|
||||
|| rawRequestBodyBuildError
|
||||
|| attempt.error_message
|
||||
|| attempt.error_type
|
||||
|| attempt.skip_reason
|
||||
|| upstreamResponseDisplay,
|
||||
)
|
||||
if (!hasDiagnostic) return null
|
||||
|
||||
const summary = summaryInput
|
||||
|| readStringField(rawFailureDiagnostic ?? {}, 'message')
|
||||
|| readStringField(rawRequestConversionError ?? {}, 'message')
|
||||
|| readStringField(rawRequestBodyBuildError ?? {}, 'message')
|
||||
|| (typeof attempt.error_message === 'string' ? formatAttemptErrorMessage(attempt.error_message, attempt.status_code) : '')
|
||||
|| attempt.error_type
|
||||
|| currentAttemptSkipReasonDisplay.value
|
||||
|| '未知失败'
|
||||
const breakpoint = resolveAttemptDiagnosticBreakpoint(attempt, rawMessageForBreakpoint)
|
||||
const providerFormat = readStringField(extra ?? {}, 'provider_api_format')
|
||||
const clientFormat = readStringField(extra ?? {}, 'client_api_format')
|
||||
|| (typeof props.requestApiFormat === 'string' ? props.requestApiFormat : '')
|
||||
const conversionDisplay = clientFormat || providerFormat
|
||||
? `${clientFormat ? formatApiFormat(clientFormat) : '未知请求格式'} → ${providerFormat ? formatApiFormat(providerFormat) : '未知上游格式'}`
|
||||
: currentAttemptFormatDisplay.value
|
||||
const analysisHint = (() => {
|
||||
const raw = `${summary}\n${rawMessageForBreakpoint}\n${attempt.error_message ?? ''}`.toLowerCase()
|
||||
if (raw.includes('unsupported provider stream event')) {
|
||||
return '断点在上游流式事件解析/转换矩阵:先按 breakpoint 对应字段确认 event type,再决定是补 canonical mapping 还是加入 known noop。'
|
||||
}
|
||||
if (raw.includes('finish reason')) {
|
||||
return '断点在 finish_reason 映射:确认该结束原因是否可等价映射;不能无损映射时保持失败闭合。'
|
||||
}
|
||||
if (raw.includes('lossy conversion') || raw.includes('无损') || raw.includes('丢失信息') || raw.includes('request_conversion')) {
|
||||
return '断点在请求/响应格式转换器:检查 breakpoint 字段是否能被目标格式表达,不能表达就需要拒绝、降级或新增显式映射策略。'
|
||||
}
|
||||
return '先从 breakpoint 字段开始回放;若 breakpoint 为 $,优先查看 raw.failure_diagnostic / raw.error_message 和 upstream_response。'
|
||||
})()
|
||||
|
||||
const payload = {
|
||||
summary,
|
||||
breakpoint,
|
||||
analysis_hint: analysisHint,
|
||||
request: {
|
||||
request_id: trace.value?.request_id ?? attempt.request_id,
|
||||
path: currentAttemptRequestPathDisplay.value || null,
|
||||
format_conversion: conversionDisplay || null,
|
||||
client_api_format: clientFormat || null,
|
||||
provider_api_format: providerFormat || null,
|
||||
needs_conversion: extra?.needs_conversion ?? null,
|
||||
conversion_mode: readStringField(extra ?? {}, 'conversion_mode') ?? null,
|
||||
},
|
||||
node: {
|
||||
candidate_id: attempt.id,
|
||||
candidate_index: attempt.candidate_index,
|
||||
retry_index: attempt.retry_index,
|
||||
provider: attempt.provider_name || attempt.provider_id || null,
|
||||
key: currentAttemptKeyDisplay.value || null,
|
||||
status: attempt.status,
|
||||
status_code: statusCode ?? attempt.status_code ?? null,
|
||||
skip_reason: attempt.skip_reason ?? null,
|
||||
error_type: attempt.error_type ?? null,
|
||||
error_message: attempt.error_message ?? null,
|
||||
},
|
||||
raw: {
|
||||
failure_diagnostic: rawFailureDiagnostic,
|
||||
request_conversion_error: rawRequestConversionError,
|
||||
request_body_build_error: rawRequestBodyBuildError,
|
||||
error_flow: extractObject(extra?.error_flow),
|
||||
upstream_response: upstreamResponseDisplay ?? normalizeUpstreamResponseDisplay(extra?.upstream_response),
|
||||
},
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
const currentAttemptExtraDataDisplay = computed<Record<string, unknown> | null>(() => {
|
||||
const extra = extractObject(currentAttempt.value?.extra_data)
|
||||
if (!extra) return null
|
||||
@@ -2765,6 +3110,7 @@ function getDisplayStatus(attempt: CandidateRecord | null | undefined): string {
|
||||
/* 跳过原因 */
|
||||
.skip-reason {
|
||||
margin-top: 1rem;
|
||||
padding: 0.75rem;
|
||||
background: hsl(var(--muted) / 0.5);
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
|
||||
@@ -570,6 +570,176 @@ describe('HorizontalRequestTimeline', () => {
|
||||
expect(root.textContent).not.toContain('该错误被标记为敏感上游错误')
|
||||
})
|
||||
|
||||
it('keeps local sync diagnostics visible when upstream response body capture is disabled', async () => {
|
||||
const trace = buildTrace([
|
||||
buildCandidate({
|
||||
id: 'cand-local-sync-diagnostic',
|
||||
provider_id: 'provider-local-sync',
|
||||
provider_name: 'Provider Local Sync',
|
||||
key_id: 'key-local-sync',
|
||||
key_name: 'Local Sync Key',
|
||||
candidate_index: 0,
|
||||
status: 'failed',
|
||||
status_code: 500,
|
||||
error_type: 'local_sync_attempt_aborted',
|
||||
error_message: 'Local sync attempt failed before terminal finalization: Internal("Unsupported provider stream event cannot be converted losslessly: field $.type = \\"response.future.delta\\"; fields: payload, response, type")',
|
||||
extra_data: {
|
||||
upstream_response: {
|
||||
status_code: 500,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body_state: 'disabled',
|
||||
},
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
const root = mountTimeline(trace)
|
||||
await nextTick()
|
||||
|
||||
expect(root.textContent).toContain('错误信息')
|
||||
expect(root.textContent).toContain('HTTP 500')
|
||||
expect(root.textContent).toContain('流式格式转换失败')
|
||||
expect(root.textContent).toContain('上游返回了当前不支持的 stream event')
|
||||
expect(root.textContent).toContain('字段 $.type = "response.future.delta"')
|
||||
const errorJsonText = root.querySelector('.error-block .error-json')?.textContent ?? ''
|
||||
expect(errorJsonText).toContain('"body_state":"disabled"')
|
||||
expect(errorJsonText).toContain('"diagnostic"')
|
||||
expect(errorJsonText).toContain('"breakpoint":"$.type"')
|
||||
expect(errorJsonText).toContain('"analysis_hint"')
|
||||
expect(errorJsonText).toContain('"raw"')
|
||||
})
|
||||
|
||||
it('formats request conversion diagnostics with field paths on skipped trace nodes', async () => {
|
||||
const trace = buildTrace([
|
||||
buildCandidate({
|
||||
id: 'cand-request-conversion',
|
||||
provider_id: 'provider-request-conversion',
|
||||
provider_name: 'Provider Request Conversion',
|
||||
key_id: 'key-request-conversion',
|
||||
key_name: 'Request Conversion Key',
|
||||
candidate_index: 0,
|
||||
status: 'skipped',
|
||||
skip_reason: 'provider_request_body_build_failed',
|
||||
extra_data: {
|
||||
failure_diagnostic: {
|
||||
kind: 'request_conversion',
|
||||
path: '$.n',
|
||||
message: 'lossy conversion blocked from openai:chat to openai:responses at n: multiple completions cannot be represented losslessly',
|
||||
safe_to_show: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
const root = mountTimeline(trace)
|
||||
await nextTick()
|
||||
|
||||
expect(root.textContent).toContain('跳过原因')
|
||||
expect(root.textContent).toContain('上游请求体转换失败')
|
||||
expect(root.textContent).toContain('$.n')
|
||||
expect(root.textContent).toContain('格式转换失败')
|
||||
expect(root.textContent).toContain('OpenAI Chat → OpenAI Responses')
|
||||
expect(root.textContent).toContain('字段 $.n 会丢失信息')
|
||||
expect(root.querySelector('.diagnostic-json-panel')).toBeNull()
|
||||
})
|
||||
|
||||
it('formats unsupported stream finish reasons with the failing field', async () => {
|
||||
const trace = buildTrace([
|
||||
buildCandidate({
|
||||
id: 'cand-finish-reason',
|
||||
provider_id: 'provider-finish-reason',
|
||||
provider_name: 'Provider Finish Reason',
|
||||
key_id: 'key-finish-reason',
|
||||
key_name: 'Finish Reason Key',
|
||||
candidate_index: 0,
|
||||
status: 'failed',
|
||||
status_code: 500,
|
||||
error_message: 'Internal("Unsupported provider stream finish reason cannot be converted losslessly: field $.finish_reason = \\"future_reason\\"")',
|
||||
}),
|
||||
])
|
||||
|
||||
const root = mountTimeline(trace)
|
||||
await nextTick()
|
||||
|
||||
expect(root.textContent).toContain('流式格式转换失败')
|
||||
expect(root.textContent).toContain('finish reason')
|
||||
expect(root.textContent).toContain('字段 $.finish_reason = "future_reason"')
|
||||
const errorJsonText = root.querySelector('.error-block .error-json')?.textContent ?? ''
|
||||
expect(errorJsonText).toContain('"diagnostic"')
|
||||
expect(errorJsonText).toContain('"breakpoint":"$.finish_reason"')
|
||||
})
|
||||
|
||||
it('uses conversion messages from error_flow as the diagnostic breakpoint source', async () => {
|
||||
const trace = buildTrace([
|
||||
buildCandidate({
|
||||
id: 'cand-error-flow-conversion',
|
||||
provider_id: 'provider-error-flow-conversion',
|
||||
provider_name: 'Provider Error Flow Conversion',
|
||||
key_id: 'key-error-flow-conversion',
|
||||
key_name: 'Error Flow Conversion Key',
|
||||
candidate_index: 0,
|
||||
status: 'failed',
|
||||
status_code: 500,
|
||||
error_message: 'execution runtime stream returned non-success status 500',
|
||||
extra_data: {
|
||||
upstream_response: {
|
||||
status_code: 500,
|
||||
body_state: 'disabled',
|
||||
},
|
||||
error_flow: {
|
||||
status_code: 500,
|
||||
message: 'lossy conversion blocked from openai:chat to openai:responses at n: multiple completions cannot be represented losslessly',
|
||||
},
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
const root = mountTimeline(trace)
|
||||
await nextTick()
|
||||
|
||||
expect(root.textContent).toContain('格式转换失败')
|
||||
expect(root.textContent).toContain('OpenAI Chat → OpenAI Responses')
|
||||
expect(root.textContent).toContain('字段 $.n 会丢失信息')
|
||||
expect(root.textContent).not.toContain('上游返回非成功状态 500')
|
||||
const errorJsonText = root.querySelector('.error-block .error-json')?.textContent ?? ''
|
||||
expect(errorJsonText).toContain('"body_state":"disabled"')
|
||||
expect(errorJsonText).toContain('"breakpoint":"$.n"')
|
||||
expect(errorJsonText).toContain('断点在请求/响应格式转换器')
|
||||
})
|
||||
|
||||
it('shows failed diagnostic messages even when the only response panel data is diagnostic metadata', async () => {
|
||||
const trace = buildTrace([
|
||||
buildCandidate({
|
||||
id: 'cand-diagnostic-only',
|
||||
provider_id: 'provider-diagnostic-only',
|
||||
provider_name: 'Provider Diagnostic Only',
|
||||
key_id: 'key-diagnostic-only',
|
||||
key_name: 'Diagnostic Only Key',
|
||||
candidate_index: 0,
|
||||
status: 'failed',
|
||||
error_type: 'request_conversion_failed',
|
||||
extra_data: {
|
||||
failure_diagnostic: {
|
||||
kind: 'request_conversion',
|
||||
path: '$.temperature',
|
||||
message: 'unsupported field temperature in openai:responses: temperature cannot be represented',
|
||||
safe_to_show: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
const root = mountTimeline(trace)
|
||||
await nextTick()
|
||||
|
||||
expect(root.textContent).toContain('错误信息')
|
||||
expect(root.textContent).toContain('格式转换失败')
|
||||
expect(root.textContent).toContain('OpenAI Responses 不支持字段 $.temperature')
|
||||
const errorJsonText = root.querySelector('.error-block .error-json')?.textContent ?? ''
|
||||
expect(errorJsonText).toContain('"diagnostic"')
|
||||
expect(errorJsonText).toContain('"breakpoint":"$.temperature"')
|
||||
})
|
||||
|
||||
it('keeps the failure message when upstream response only records an empty body state', async () => {
|
||||
const trace = buildTrace([
|
||||
buildCandidate({
|
||||
|
||||
Reference in New Issue
Block a user