优化agents.ts

This commit is contained in:
jingrow 2025-11-05 01:56:22 +08:00
parent 1dffd4194a
commit fe70634f98

View File

@ -1,32 +1,9 @@
import axios from 'axios'
import type { AIAgent } from '../types'
import { getRecord } from './common'
import { getRecord, updateRecord } from './common'
// 重新导出类型,供其他模块使用
export type { AIAgent }
// 统一使用相对路径,通过 Vite 代理转发到后端
// 获取Jingrow API鉴权头部
function get_jingrow_api_headers() {
const apiKey = (import.meta as any).env.VITE_JINGROW_API_KEY
const apiSecret = (import.meta as any).env.VITE_JINGROW_API_SECRET
// 如果没有设置API密钥暂时返回基本头部
if (!apiKey || !apiSecret) {
console.warn('API密钥未设置使用基本头部')
return {
'Content-Type': 'application/json'
}
}
return {
'Authorization': `token ${apiKey}:${apiSecret}`,
'Content-Type': 'application/json'
}
}
// 获取单个AI Agent详情
export const getAgentDetail = async (name: string): Promise<AIAgent> => {
try {
@ -45,32 +22,15 @@ export const getAgentDetail = async (name: string): Promise<AIAgent> => {
// 更新AI Agent
export const updateAgentApi = async (name: string, data: Partial<AIAgent>): Promise<AIAgent> => {
try {
const response = await axios.post(
`/api/action/jingrow.ai.utils.jlocal.update_local_ai_agent`,
{
name: name,
data: data
},
{
headers: get_jingrow_api_headers(),
withCredentials: true
}
)
const result = await updateRecord('Local Ai Agent', name, data)
const message = response.data.message
if (message?.error) {
const errorMsg = typeof message.error === 'object' ? JSON.stringify(message.error) : message.error
throw new Error(errorMsg)
if (!result.success) {
throw new Error(result.message || '更新AI Agent失败')
}
// 检查更新是否成功
if (!message?.success) {
throw new Error(message?.message || '更新失败')
}
return message
return result.data
} catch (error: any) {
console.error("Error in updateAgentApi:", error)
throw new Error(error.response?.data?.message || error.message || '更新AI Agent失败')
throw new Error(error.message || '更新AI Agent失败')
}
}