优化agents.ts
This commit is contained in:
parent
1dffd4194a
commit
fe70634f98
@ -1,32 +1,9 @@
|
|||||||
import axios from 'axios'
|
|
||||||
|
|
||||||
import type { AIAgent } from '../types'
|
import type { AIAgent } from '../types'
|
||||||
import { getRecord } from './common'
|
import { getRecord, updateRecord } from './common'
|
||||||
|
|
||||||
// 重新导出类型,供其他模块使用
|
// 重新导出类型,供其他模块使用
|
||||||
export type { AIAgent }
|
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详情
|
// 获取单个AI Agent详情
|
||||||
export const getAgentDetail = async (name: string): Promise<AIAgent> => {
|
export const getAgentDetail = async (name: string): Promise<AIAgent> => {
|
||||||
try {
|
try {
|
||||||
@ -45,32 +22,15 @@ export const getAgentDetail = async (name: string): Promise<AIAgent> => {
|
|||||||
// 更新AI Agent
|
// 更新AI Agent
|
||||||
export const updateAgentApi = async (name: string, data: Partial<AIAgent>): Promise<AIAgent> => {
|
export const updateAgentApi = async (name: string, data: Partial<AIAgent>): Promise<AIAgent> => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const result = await updateRecord('Local Ai Agent', name, data)
|
||||||
`/api/action/jingrow.ai.utils.jlocal.update_local_ai_agent`,
|
|
||||||
{
|
|
||||||
name: name,
|
|
||||||
data: data
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: get_jingrow_api_headers(),
|
|
||||||
withCredentials: true
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const message = response.data.message
|
if (!result.success) {
|
||||||
if (message?.error) {
|
throw new Error(result.message || '更新AI Agent失败')
|
||||||
const errorMsg = typeof message.error === 'object' ? JSON.stringify(message.error) : message.error
|
|
||||||
throw new Error(errorMsg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查更新是否成功
|
return result.data
|
||||||
if (!message?.success) {
|
|
||||||
throw new Error(message?.message || '更新失败')
|
|
||||||
}
|
|
||||||
|
|
||||||
return message
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error("Error in updateAgentApi:", error)
|
console.error("Error in updateAgentApi:", error)
|
||||||
throw new Error(error.response?.data?.message || error.message || '更新AI Agent失败')
|
throw new Error(error.message || '更新AI Agent失败')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user