Files
xiaoyin-gateway/frontend/src/lib/agents.ts
T
api-gateway 678b984b95 feat(agents): 模型改为页面可选(方案 A)+ 修引擎模型读不到的静默 bug(版本 2.5.4 → 2.6.0)
推翻旧结论:模型不是全局唯一,而是每个 profile 各存一份 active_model
(PUT /api/models/active scope=agent → workspaces/<profile>/agent.json),
改完即时生效不用重启 —— 实测切模型后真对话 turn_usage 的 model_name 确实变了。

真 bug:refresh_engine_live() 读的是 /api/providers/active,那条是 404,
异常被吞 → engine_live['model'] 恒为空串 → 配置页模型栏一直显示「—」。
改用 ACTIVE_MODEL_PATH=/api/models/active + get_active_model()。

新增:
- 适配器 get_active_model / set_active_model(4xx 抛 EngineError 并透传引擎原文)
- GET /agents/engine/models(只列真能用的;实时 current,不走 TTL 缓存)
- PUT /agents/engine/model(带防自指护栏,不得指向 agent/*)
- 前端配置页模型栏:只读 → 可搜索下拉(按供应商分组 + 视觉标)+ 刷新;失败回滚
- i18n 删过期键、加 5 新键(中英 883/883 对称)
- 测试 +5(端点/路径常量回归/body 与头/4xx 抛错/防自指),224 passed

语义:方案 A = 所有智能体共用这一个模型(一个 profile 一个 active_model),
做不到 A 用 flash 且 B 用 pro 并存;那需方案 B(一智能体一 profile)。
2026-09-30 14:40:58 +08:00

168 lines
5.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 智能体(第三十八轮 · 路线 A)—— 接口封装 + SSE 解析。
//
// 设计见 docs/agents-dev-plan.md;SSE 协议见 docs/qwenpaw-agent-api-sse.md。
// 后端把 QwenPaw 的事件翻译成这几种(前端不必懂 AgentScope):
import { getToken } from '../api'
export interface AgentItem {
slug: string
display_name: string
description: string
params?: { temperature?: number; max_tokens?: number; top_p?: number; system_suffix?: string }
engine?: { type: string; base_url: string; agent_id: string }
access?: { roles: string[]; users: string[] }
status: 'draft' | 'published' | 'retired'
created_at?: string
updated_at?: string
access_scope?: string
last_used_at?: string
run_total?: number
engine_model?: string
engine_ready?: boolean
}
export interface AgentListResp {
agents: AgentItem[]
max_agents: number
active: number
engine: { model: string; ready: boolean; base_url: string; agent_id: string }
}
export interface AgentRun {
at: string
account: string
rounds: number
tools: number
tokens: number
ms: number
status: string
}
/** 后端 SSE 的事件类型。 */
export type AgentEvent =
| { type: 'start'; slug: string; session_id: string }
| { type: 'text'; text: string }
| { type: 'step'; id: string; name?: string; args?: any; status: 'running' | 'done'; ms?: number; output?: any }
| { type: 'usage'; prompt: number; completion: number; cached: number; model: string; provider: string; context_ratio?: number }
| { type: 'done'; status: string }
| { type: 'error'; message: string }
function authHeaders(): Record<string, string> {
return { Authorization: `Bearer ${getToken()}` }
}
async function req<T>(path: string, init: RequestInit = {}): Promise<T> {
const r = await fetch(path, {
...init,
headers: { 'Content-Type': 'application/json', ...authHeaders(), ...(init.headers || {}) },
})
const body = await r.json().catch(() => null)
if (!r.ok) {
const msg = body?.error?.message || body?.detail || `HTTP ${r.status}`
throw new Error(typeof msg === 'string' ? msg : JSON.stringify(msg))
}
return body as T
}
export const listAgents = (includeRetired = false) =>
req<AgentListResp>(`/agents${includeRetired ? '?include_retired=true' : ''}`)
export const createAgent = (body: Record<string, any>) =>
req<{ status: string; agent: AgentItem }>('/agents', { method: 'POST', body: JSON.stringify(body) })
export const updateAgent = (slug: string, patch: Record<string, any>) =>
req<{ status: string; agent: AgentItem }>(`/agents/${slug}`, { method: 'PUT', body: JSON.stringify(patch) })
export const retireAgent = (slug: string) =>
req<{ status: string }>(`/agents/${slug}`, { method: 'DELETE' })
export const restoreAgent = (slug: string) =>
req<{ status: string; agent: AgentItem }>(`/agents/${slug}/restore`, { method: 'POST' })
export const agentRuns = (slug: string) =>
req<{ slug: string; total: number; last_used_at: string; items: AgentRun[] }>(`/agents/${slug}/runs`)
export const testAgent = (slug: string) =>
req<{ ok: boolean; ms: number; detail: string }>(`/agents/${slug}/test`, { method: 'POST' })
export const agentSessions = (slug: string) =>
req<{ slug: string; kind: string; items: any[] }>(`/agents/${slug}/sessions`)
/** 引擎可选模型清单 + 当前生效模型(第四十轮 · 方案 A:所有智能体共用)。 */
export interface EngineModelOption {
provider_id: string
provider_name: string
model: string
vision: boolean
}
export interface EngineModelsResp {
options: EngineModelOption[]
current: { provider_id: string; model: string }
engine: { base_url: string; agent_id: string; ready: boolean }
scope_note: string
}
export const engineModels = () => req<EngineModelsResp>('/agents/engine/models')
/** 切换引擎模型(即时生效,不用重启)。 */
export const setEngineModel = (providerId: string, model: string) =>
req<{ status: string; current: { provider_id: string; model: string } }>(
'/agents/engine/model',
{ method: 'PUT', body: JSON.stringify({ provider_id: providerId, model }) },
)
export const stopAgent = (slug: string, chatId: string) =>
req<{ status: string }>(`/agents/${slug}/stop?chat_id=${encodeURIComponent(chatId)}`, { method: 'POST' })
/**
* 与智能体对话(SSE 流式)。逐事件回调 —— 与 Chat.vue 的流式处理同款思路。
*
* 返回一个 abort 函数(停止按钮用)。注意:网关侧的"停止"是转发给引擎的
* /console/chat/stop;前端 abort 只是断开本地连接,两者可同时用。
*/
export function streamAgentChat(
slug: string,
messages: { role: string; content: string | any[] }[],
sessionId: string,
onEvent: (ev: AgentEvent) => void,
): () => void {
const ctrl = new AbortController()
;(async () => {
try {
const r = await fetch(`/agents/${slug}/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...authHeaders() },
body: JSON.stringify({ messages, session_id: sessionId }),
signal: ctrl.signal,
})
if (!r.ok) {
const body = await r.json().catch(() => null)
const msg = body?.error?.message || body?.detail || `HTTP ${r.status}`
onEvent({ type: 'error', message: String(msg) })
return
}
const reader = r.body!.getReader()
const dec = new TextDecoder()
let buf = ''
for (;;) {
const { done, value } = await reader.read()
if (done) break
buf += dec.decode(value, { stream: true })
const parts = buf.split('\n\n')
buf = parts.pop() || ''
for (const part of parts) {
const line = part.trim()
if (!line.startsWith('data:')) continue
try {
onEvent(JSON.parse(line.slice(5).trim()) as AgentEvent)
} catch { /* 忽略坏帧 */ }
}
}
} catch (e: any) {
if (e?.name !== 'AbortError') onEvent({ type: 'error', message: String(e?.message || e) })
}
})()
return () => ctrl.abort()
}