mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 22:52:08 +08:00
feat(api): add models endpoint to agent client and type response
Add new models endpoint to AgentApiClient with query parameter support Ensure type safety by adding ApiModelsResponse type and schema validation
This commit is contained in:
parent
c426876d0d
commit
c77d7dff78
@ -1,4 +1,4 @@
|
|||||||
import { ApiModelsFilterSchema } from '@types'
|
import { ApiModelsFilterSchema, ApiModelsResponse } from '@types'
|
||||||
import express, { Request, Response } from 'express'
|
import express, { Request, Response } from 'express'
|
||||||
|
|
||||||
import { loggerService } from '../../services/LoggerService'
|
import { loggerService } from '../../services/LoggerService'
|
||||||
@ -114,7 +114,7 @@ const router = express
|
|||||||
response.data.map((m) => m.id)
|
response.data.map((m) => m.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
return res.json(response)
|
return res.json(response satisfies ApiModelsResponse)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.error('Error fetching models:', error)
|
logger.error('Error fetching models:', error)
|
||||||
return res.status(503).json({
|
return res.status(503).json({
|
||||||
|
|||||||
@ -3,6 +3,8 @@ import { formatAgentServerError } from '@renderer/utils'
|
|||||||
import {
|
import {
|
||||||
AddAgentForm,
|
AddAgentForm,
|
||||||
AgentServerErrorSchema,
|
AgentServerErrorSchema,
|
||||||
|
ApiModelsResponse,
|
||||||
|
ApiModelsResponseSchema,
|
||||||
CreateAgentRequest,
|
CreateAgentRequest,
|
||||||
CreateAgentResponse,
|
CreateAgentResponse,
|
||||||
CreateAgentResponseSchema,
|
CreateAgentResponseSchema,
|
||||||
@ -18,6 +20,8 @@ import {
|
|||||||
ListAgentSessionsResponseSchema,
|
ListAgentSessionsResponseSchema,
|
||||||
type ListAgentsResponse,
|
type ListAgentsResponse,
|
||||||
ListAgentsResponseSchema,
|
ListAgentsResponseSchema,
|
||||||
|
objectEntries,
|
||||||
|
objectKeys,
|
||||||
UpdateAgentForm,
|
UpdateAgentForm,
|
||||||
UpdateAgentRequest,
|
UpdateAgentRequest,
|
||||||
UpdateAgentResponse,
|
UpdateAgentResponse,
|
||||||
@ -77,6 +81,19 @@ export class AgentApiClient {
|
|||||||
public getSessionMessagesPath = (agentId: string, sessionId: string) =>
|
public getSessionMessagesPath = (agentId: string, sessionId: string) =>
|
||||||
`/${this.apiVersion}/agents/${agentId}/sessions/${sessionId}/messages`
|
`/${this.apiVersion}/agents/${agentId}/sessions/${sessionId}/messages`
|
||||||
|
|
||||||
|
public modelsPath = (props?: { providerType?: 'anthropic'; limit?: number }) => {
|
||||||
|
const base = `/${this.apiVersion}/models`
|
||||||
|
if (!props) return base
|
||||||
|
if (objectKeys(props).length > 0) {
|
||||||
|
const params = objectEntries(props)
|
||||||
|
.map(([key, value]) => `${key}=${value}`)
|
||||||
|
.join('&')
|
||||||
|
return `${base}?${params}`
|
||||||
|
} else {
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async listAgents(): Promise<ListAgentsResponse> {
|
public async listAgents(): Promise<ListAgentsResponse> {
|
||||||
const url = this.agentPaths.base
|
const url = this.agentPaths.base
|
||||||
try {
|
try {
|
||||||
@ -216,4 +233,15 @@ export class AgentApiClient {
|
|||||||
throw processError(error, 'Failed to post message.')
|
throw processError(error, 'Failed to post message.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getModels(props?: { providerType?: 'anthropic'; limit?: number }): Promise<ApiModelsResponse> {
|
||||||
|
const url = this.modelsPath(props)
|
||||||
|
try {
|
||||||
|
const response = await this.axios.get(url)
|
||||||
|
const data = ApiModelsResponseSchema.parse(response.data)
|
||||||
|
return data
|
||||||
|
} catch (error) {
|
||||||
|
throw processError(error, 'Failed to get models.')
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user